Skip to content

Commit

Permalink
Add a note to unused variables
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Aug 16, 2017
1 parent b617960 commit 9b0607a
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions src/librustc/middle/liveness.rs
Expand Up @@ -1482,12 +1482,16 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
};

if is_assigned {
self.ir.tcx.lint_node(lint::builtin::UNUSED_VARIABLES, id, sp,
self.ir.tcx.lint_node_note(lint::builtin::UNUSED_VARIABLES, id, sp,
&format!("variable `{}` is assigned to, but never used",
name),
&format!("to disable this warning, consider using `_{}` instead",
name));
} else if name != "self" {
self.ir.tcx.lint_node(lint::builtin::UNUSED_VARIABLES, id, sp,
&format!("unused variable: `{}`", name));
self.ir.tcx.lint_node_note(lint::builtin::UNUSED_VARIABLES, id, sp,
&format!("unused variable: `{}`", name),
&format!("to disable this warning, consider using `_{}` instead",
name));
}
}
true
Expand Down
11 changes: 11 additions & 0 deletions src/librustc/ty/context.rs
Expand Up @@ -1522,6 +1522,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
self.struct_span_lint_node(lint, id, span.into(), msg).emit()
}

pub fn lint_node_note<S: Into<MultiSpan>>(self,
lint: &'static Lint,
id: NodeId,
span: S,
msg: &str,
note: &str) {
let mut err = self.struct_span_lint_node(lint, id, span.into(), msg);
err.note(note);
err.emit()
}

pub fn lint_level_at_node(self, lint: &'static Lint, mut id: NodeId)
-> (lint::Level, lint::LintSource)
{
Expand Down
1 change: 1 addition & 0 deletions src/test/ui/span/issue-24690.stderr
Expand Up @@ -10,6 +10,7 @@ note: lint level defined here
18 | #![warn(unused)]
| ^^^^^^
= note: #[warn(unused_variables)] implied by #[warn(unused)]
= note: to disable this warning, consider using `_theOtherTwo` instead

warning: variable `theTwo` should have a snake case name such as `the_two`
--> $DIR/issue-24690.rs:22:9
Expand Down

0 comments on commit 9b0607a

Please sign in to comment.