Skip to content

Commit c886246

Browse files
committed
Auto merge of #43850 - GuillaumeGomez:unused-variable-lint, r=arielb1
Add a note to unused variables Fixes #26720.
2 parents 85eadf8 + 9b0607a commit c886246

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
@@ -1840,6 +1840,17 @@ impl<'a, 'gcx, 'tcx> TyCtxt<'a, 'gcx, 'tcx> {
18401840
self.struct_span_lint_node(lint, id, span.into(), msg).emit()
18411841
}
18421842

1843+
pub fn lint_node_note<S: Into<MultiSpan>>(self,
1844+
lint: &'static Lint,
1845+
id: NodeId,
1846+
span: S,
1847+
msg: &str,
1848+
note: &str) {
1849+
let mut err = self.struct_span_lint_node(lint, id, span.into(), msg);
1850+
err.note(note);
1851+
err.emit()
1852+
}
1853+
18431854
pub fn lint_level_at_node(self, lint: &'static Lint, mut id: NodeId)
18441855
-> (lint::Level, lint::LintSource)
18451856
{

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)