Skip to content

Commit b961285

Browse files
committed
Change check_must_use to return string
1 parent 1727dee commit b961285

File tree

1 file changed

+27
-24
lines changed

1 file changed

+27
-24
lines changed

src/librustc_lint/unused.rs

Lines changed: 27 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,8 @@ impl LintPass for UnusedResults {
120120
}
121121

122122
impl LateLintPass for UnusedResults {
123-
fn check_stmt(&mut self, cx: &LateContext, s: &hir::Stmt) {
124-
let expr = match s.node {
123+
fn check_stmt(&mut self, cx: &LateContext, stmt: &hir::Stmt) {
124+
let expr = match stmt.node {
125125
hir::StmtSemi(ref expr, _) => &**expr,
126126
_ => return
127127
};
@@ -131,37 +131,40 @@ impl LateLintPass for UnusedResults {
131131
}
132132

133133
let t = cx.tcx.expr_ty(&expr);
134-
let warned = match t.sty {
134+
match t.sty {
135135
ty::TyTuple(ref tys) if tys.is_empty() => return,
136136
ty::TyBool => return,
137-
ty::TyStruct(def, _) |
138-
ty::TyEnum(def, _) => {
139-
let attrs = cx.tcx.get_attrs(def.did);
140-
check_must_use(cx, &attrs[..], s.span)
137+
_ => ()
138+
}
139+
let must_use = check_must_use(cx, t);
140+
if let Some(s) = must_use {
141+
let mut msg = "unused result which must be used".to_string();
142+
if !s.is_empty() {
143+
msg.push_str(": ");
144+
msg.push_str(&s);
141145
}
142-
_ => false,
143-
};
144-
if !warned {
145-
cx.span_lint(UNUSED_RESULTS, s.span, "unused result");
146+
cx.span_lint(UNUSED_MUST_USE, stmt.span, &msg);
147+
} else {
148+
cx.span_lint(UNUSED_RESULTS, stmt.span, "unused result");
146149
}
147150

148-
fn check_must_use(cx: &LateContext, attrs: &[ast::Attribute], sp: Span) -> bool {
149-
for attr in attrs {
150-
if attr.check_name("must_use") {
151-
let mut msg = "unused result which must be used".to_string();
152-
// check for #[must_use="..."]
153-
match attr.value_str() {
154-
None => {}
155-
Some(s) => {
156-
msg.push_str(": ");
157-
msg.push_str(&s);
151+
fn check_must_use(cx: &LateContext, ty: ty::Ty) -> Option<String> {
152+
match ty.sty {
153+
ty::TyStruct(def, _) |
154+
ty::TyEnum(def, _) => {
155+
for attr in cx.tcx.get_attrs(def.did).iter() {
156+
if attr.check_name("must_use") {
157+
let s = match attr.value_str() {
158+
None => "".to_string(),
159+
Some(s) => s.to_string()
160+
};
161+
return Some(s);
158162
}
159163
}
160-
cx.span_lint(UNUSED_MUST_USE, sp, &msg);
161-
return true;
162164
}
165+
_ => ()
163166
}
164-
false
167+
None
165168
}
166169
}
167170
}

0 commit comments

Comments
 (0)