-
Notifications
You must be signed in to change notification settings - Fork 13.1k
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
correct unused field pattern suggestions #47922
Merged
bors
merged 2 commits into
rust-lang:master
from
zackmdavis:and_the_case_of_the_unused_field_pattern
Feb 7, 2018
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
34 changes: 34 additions & 0 deletions
34
src/test/ui/lint/issue-47390-unused-variable-in-struct-pattern.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT | ||
// file at the top-level directory of this distribution and at | ||
// http://rust-lang.org/COPYRIGHT. | ||
// | ||
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or | ||
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license | ||
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your | ||
// option. This file may not be copied, modified, or distributed | ||
// except according to those terms. | ||
|
||
// must-compile-successfully | ||
|
||
#![warn(unused)] // UI tests pass `-A unused` (#43896) | ||
|
||
struct SoulHistory { | ||
corridors_of_light: usize, | ||
hours_are_suns: bool, | ||
endless_and_singing: bool | ||
} | ||
|
||
fn main() { | ||
let i_think_continually = 2; | ||
let who_from_the_womb_remembered = SoulHistory { | ||
corridors_of_light: 5, | ||
hours_are_suns: true, | ||
endless_and_singing: true | ||
}; | ||
|
||
if let SoulHistory { corridors_of_light, | ||
mut hours_are_suns, | ||
endless_and_singing: true } = who_from_the_womb_remembered { | ||
hours_are_suns = false; | ||
} | ||
} |
40 changes: 40 additions & 0 deletions
40
src/test/ui/lint/issue-47390-unused-variable-in-struct-pattern.stderr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
warning: unused variable: `i_think_continually` | ||
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:22:9 | ||
| | ||
22 | let i_think_continually = 2; | ||
| ^^^^^^^^^^^^^^^^^^^ help: consider using `_i_think_continually` instead | ||
| | ||
note: lint level defined here | ||
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:13:9 | ||
| | ||
13 | #![warn(unused)] // UI tests pass `-A unused` (#43896) | ||
| ^^^^^^ | ||
= note: #[warn(unused_variables)] implied by #[warn(unused)] | ||
|
||
warning: unused variable: `corridors_of_light` | ||
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:29:26 | ||
| | ||
29 | 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:30:26 | ||
| | ||
30 | 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:32:9 | ||
| | ||
32 | hours_are_suns = false; | ||
| ^^^^^^^^^^^^^^ | ||
| | ||
note: lint level defined here | ||
--> $DIR/issue-47390-unused-variable-in-struct-pattern.rs:13:9 | ||
| | ||
13 | #![warn(unused)] // UI tests pass `-A unused` (#43896) | ||
| ^^^^^^ | ||
= note: #[warn(unused_assignments)] implied by #[warn(unused)] | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This warning is emitted somewhere else. Would you mind checking how hard it'd be to modify that note by checking if it is a field in a struct literal?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@estebank The PR touches this code (on master, the note starts with "to avoid this warning", which I thought was unnecessary). I had started working on making this a structured suggestion, too, but then backed off when I realized that getting the suggestion right would involve more refactoring than I wanted to do now. Notice that the span includes the
mut
: in the case of an entirely unused variable, that's fine (the mut is also unused, so replacing both the variable and anymut
s orref
s withvariable: _
is correct), but in the case of a mere unused assignment (where we mutate the variable, but don't do anything interesting with it), not only is themut
significant, but there are also other appearances of the variable (requiring a multi-span suggestion).