Skip to content

Reduce span highlighted code in unused_variables lint #50675

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
May 14, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/librustc/hir/pat_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,15 @@ impl hir::Pat {
}
}

pub fn simple_span(&self) -> Option<Span> {
match self.node {
PatKind::Binding(hir::BindingAnnotation::Unannotated, _, ref path1, None) |
PatKind::Binding(hir::BindingAnnotation::Mutable, _, ref path1, None) =>
Some(path1.span),
_ => None,
}
}

/// Return variants that are necessary to exist for the pattern to match.
pub fn necessary_variants(&self) -> Vec<DefId> {
let mut variants = vec![];
Expand Down
17 changes: 10 additions & 7 deletions src/librustc/middle/liveness.rs
Original file line number Diff line number Diff line change
Expand Up @@ -453,11 +453,11 @@ fn visit_arm<'a, 'tcx>(ir: &mut IrMaps<'a, 'tcx>, arm: &'tcx hir::Arm) {
}
}

pat.each_binding(|bm, p_id, sp, path1| {
pat.each_binding(|bm, p_id, _sp, path1| {
debug!("adding local variable {} from match with bm {:?}",
p_id, bm);
let name = path1.node;
ir.add_live_node_for_node(p_id, VarDefNode(sp));
ir.add_live_node_for_node(p_id, VarDefNode(path1.span));
ir.add_variable(Local(LocalInfo {
id: p_id,
name: name,
Expand Down Expand Up @@ -628,10 +628,10 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {
fn pat_bindings<F>(&mut self, pat: &hir::Pat, mut f: F) where
F: FnMut(&mut Liveness<'a, 'tcx>, LiveNode, Variable, Span, NodeId),
{
pat.each_binding(|_bm, p_id, sp, _n| {
pat.each_binding(|_bm, p_id, sp, n| {
let ln = self.live_node(p_id, sp);
let var = self.variable(p_id, sp);
f(self, ln, var, sp, p_id);
let var = self.variable(p_id, n.span);
f(self, ln, var, n.span, p_id);
})
}

Expand Down Expand Up @@ -1398,7 +1398,8 @@ fn check_local<'a, 'tcx>(this: &mut Liveness<'a, 'tcx>, local: &'tcx hir::Local)
},
None => {
this.pat_bindings(&local.pat, |this, ln, var, sp, id| {
this.warn_about_unused(sp, id, ln, var);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of the if let you could prepend this line with let sp = local.pat.simple_span.unwrap_or(sp);

let span = local.pat.simple_span().unwrap_or(sp);
this.warn_about_unused(span, id, ln, var);
})
}
}
Expand Down Expand Up @@ -1497,7 +1498,8 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {

fn warn_about_unused_args(&self, body: &hir::Body, entry_ln: LiveNode) {
for arg in &body.arguments {
arg.pat.each_binding(|_bm, p_id, sp, path1| {
arg.pat.each_binding(|_bm, p_id, _, path1| {
let sp = path1.span;
let var = self.variable(p_id, sp);
// Ignore unused self.
let name = path1.node;
Expand Down Expand Up @@ -1541,6 +1543,7 @@ impl<'a, 'tcx> Liveness<'a, 'tcx> {

let suggest_underscore_msg = format!("consider using `_{}` instead",
name);

if is_assigned {
self.ir.tcx
.lint_node_note(lint::builtin::UNUSED_VARIABLES, id, sp,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@ fn main() {
endless_and_singing: true
};

let mut mut_unused_var = 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if that works, but you can try adding a // run-rustfix comment to the top of the file. That'll show us the file after applying the suggestions


let (mut var, unused_var) = (1, 2);

if let SoulHistory { corridors_of_light,
mut hours_are_suns,
endless_and_singing: true } = who_from_the_womb_remembered {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,40 @@ LL | #![warn(unused)] // UI tests pass `-A unused` (#43896)
| ^^^^^^
= note: #[warn(unused_variables)] implied by #[warn(unused)]

warning: unused variable: `mut_unused_var`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:38:13
|
LL | let mut mut_unused_var = 1;
| ^^^^^^^^^^^^^^ help: consider using `_mut_unused_var` instead

warning: unused variable: `var`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:40:14
|
LL | let (mut var, unused_var) = (1, 2);
| ^^^ help: consider using `_var` instead

warning: unused variable: `unused_var`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:40:19
|
LL | let (mut var, unused_var) = (1, 2);
| ^^^^^^^^^^ help: consider using `_unused_var` instead

warning: unused variable: `corridors_of_light`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:38:26
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:42:26
|
LL | if let SoulHistory { corridors_of_light,
| ^^^^^^^^^^^^^^^^^^ help: try ignoring the field: `corridors_of_light: _`

warning: variable `hours_are_suns` is assigned to, but never used
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:39:26
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:43:30
|
LL | mut hours_are_suns,
| ^^^^^^^^^^^^^^^^^^
| ^^^^^^^^^^^^^^
|
= note: consider using `_hours_are_suns` instead

warning: value assigned to `hours_are_suns` is never read
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:41:9
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:45:9
|
LL | hours_are_suns = false;
| ^^^^^^^^^^^^^^
Expand All @@ -39,38 +57,61 @@ LL | #![warn(unused)] // UI tests pass `-A unused` (#43896)
= note: #[warn(unused_assignments)] implied by #[warn(unused)]

warning: unused variable: `case`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:50:23
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:54:23
|
LL | Large::Suit { case } => {}
| ^^^^ help: try ignoring the field: `case: _`

warning: unused variable: `case`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:55:24
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:59:24
|
LL | &Large::Suit { case } => {}
| ^^^^ help: try ignoring the field: `case: _`

warning: unused variable: `case`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:60:27
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:64:27
|
LL | box Large::Suit { case } => {}
| ^^^^ help: try ignoring the field: `case: _`

warning: unused variable: `case`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:65:24
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:69:24
|
LL | (Large::Suit { case },) => {}
| ^^^^ help: try ignoring the field: `case: _`

warning: unused variable: `case`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:70:24
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:74:24
|
LL | [Large::Suit { case }] => {}
| ^^^^ help: try ignoring the field: `case: _`

warning: unused variable: `case`
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:75:29
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:79:29
|
LL | Tuple(Large::Suit { case }, ()) => {}
| ^^^^ help: try ignoring the field: `case: _`

warning: variable does not need to be mutable
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:38:9
|
LL | let mut mut_unused_var = 1;
| ----^^^^^^^^^^^^^^
| |
| help: remove this `mut`
|
note: lint level defined here
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:15:9
|
LL | #![warn(unused)] // UI tests pass `-A unused` (#43896)
| ^^^^^^
= note: #[warn(unused_mut)] implied by #[warn(unused)]

warning: variable does not need to be mutable
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:40:10
|
LL | let (mut var, unused_var) = (1, 2);
| ----^^^
| |
| help: remove this `mut`