Skip to content

Commit 9b0607a

Browse files
Add a note to unused variables
1 parent b617960 commit 9b0607a

File tree

3 files changed

+19
-3
lines changed

3 files changed

+19
-3
lines changed

src/librustc/middle/liveness.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -1482,12 +1482,16 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
14821482
};
14831483

14841484
if is_assigned {
1485-
self.ir.tcx.lint_node(lint::builtin::UNUSED_VARIABLES, id, sp,
1485+
self.ir.tcx.lint_node_note(lint::builtin::UNUSED_VARIABLES, id, sp,
14861486
&format!("variable `{}` is assigned to, but never used",
1487+
name),
1488+
&format!("to disable this warning, consider using `_{}` instead",
14871489
name));
14881490
} else if name != "self" {
1489-
self.ir.tcx.lint_node(lint::builtin::UNUSED_VARIABLES, id, sp,
1490-
&format!("unused variable: `{}`", name));
1491+
self.ir.tcx.lint_node_note(lint::builtin::UNUSED_VARIABLES, id, sp,
1492+
&format!("unused variable: `{}`", name),
1493+
&format!("to disable this warning, consider using `_{}` instead",
1494+
name));
14911495
}
14921496
}
14931497
true

src/librustc/ty/context.rs

+11
Original file line numberDiff line numberDiff line change
@@ -1522,6 +1522,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
15221522
self.struct_span_lint_node(lint, id, span.into(), msg).emit()
15231523
}
15241524

1525+
pub fn lint_node_note<S: Into<MultiSpan>>(self,
1526+
lint: &'static Lint,
1527+
id: NodeId,
1528+
span: S,
1529+
msg: &str,
1530+
note: &str) {
1531+
let mut err = self.struct_span_lint_node(lint, id, span.into(), msg);
1532+
err.note(note);
1533+
err.emit()
1534+
}
1535+
15251536
pub fn lint_level_at_node(self, lint: &'static Lint, mut id: NodeId)
15261537
-> (lint::Level, lint::LintSource)
15271538
{

src/test/ui/span/issue-24690.stderr

+1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ note: lint level defined here
1010
18 | #![warn(unused)]
1111
| ^^^^^^
1212
= note: #[warn(unused_variables)] implied by #[warn(unused)]
13+
= note: to disable this warning, consider using `_theOtherTwo` instead
1314

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

0 commit comments

Comments
 (0)