-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add small underscore scope lint #3953
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
Closed
JamesHinshelwood
wants to merge
8
commits into
rust-lang:master
from
JamesHinshelwood:small-underscore-scope
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8b92b37
Add small underscore scope lint
JamesHinshelwood 6057f5d
Add known problem
JamesHinshelwood fcdff9d
Run rustfmt
JamesHinshelwood ddacccc
Fix trailing newline
JamesHinshelwood f8685b1
Fix newline again
JamesHinshelwood 5a91136
Detect more invalid patterns
JamesHinshelwood 2a1962b
Use declare_lint_pass macro
JamesHinshelwood 0ae6efe
Switch to style lint category
JamesHinshelwood 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,69 @@ | ||
use crate::utils::span_lint_and_sugg; | ||
use rustc::lint::{EarlyContext, EarlyLintPass, LintArray, LintPass}; | ||
use rustc::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_errors::Applicability; | ||
use syntax::ast::*; | ||
use syntax::source_map::Span; | ||
|
||
declare_clippy_lint! { | ||
/// **What is does:** Checks for wildcard patterns inside a struct or tuple which could instead encompass the entire pattern. | ||
/// | ||
/// **Why is this bad?** The extra binding information is meaningless and makes the code harder to read. | ||
/// | ||
/// **Known problems:** None. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust,ignore | ||
/// // Bad | ||
/// let t = (1, 2); | ||
/// let (_, _) = t; | ||
/// | ||
/// // Good | ||
/// let t = (1, 2); | ||
/// let _ = t; | ||
/// ``` | ||
pub SMALL_UNDERSCORE_SCOPE, | ||
style, | ||
"wildcard binding occurs inside a struct, but the wildcard could be the entire binding" | ||
} | ||
|
||
declare_lint_pass!(SmallUnderscoreScope => [SMALL_UNDERSCORE_SCOPE]); | ||
|
||
impl EarlyLintPass for SmallUnderscoreScope { | ||
fn check_pat(&mut self, cx: &EarlyContext<'_>, pat: &Pat, _: &mut bool) { | ||
match pat.node { | ||
PatKind::TupleStruct(_, ref pats, _) | PatKind::Tuple(ref pats, _) => { | ||
// `Foo(..)` | `(..)` | `Foo(_, _)` | `(_, _)` | ||
if pats.is_empty() | ||
|| pats.iter().all(|pat| match pat.node { | ||
PatKind::Wild => true, | ||
_ => false, | ||
}) | ||
{ | ||
emit_lint(cx, pat.span); | ||
} | ||
}, | ||
PatKind::Struct(_, ref pats, _) => { | ||
// `Bar { .. }` | ||
// The `Bar { x: _, y: _ }` is covered by `unneeded_field_pattern`, which suggests `Bar { .. }` | ||
if pats.is_empty() { | ||
emit_lint(cx, pat.span); | ||
} | ||
}, | ||
_ => (), | ||
} | ||
} | ||
} | ||
|
||
fn emit_lint(cx: &EarlyContext<'_>, sp: Span) { | ||
span_lint_and_sugg( | ||
cx, | ||
SMALL_UNDERSCORE_SCOPE, | ||
sp, | ||
"this wildcard binding could have a wider scope", | ||
"try", | ||
"_".to_string(), | ||
Applicability::MachineApplicable, | ||
) | ||
} |
This file contains hidden or 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,27 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::small_underscore_scope)] | ||
|
||
struct NewType(u8); | ||
|
||
#[allow(dead_code)] | ||
struct Foo { | ||
x: u8, | ||
y: u8, | ||
} | ||
|
||
fn main() { | ||
let n = NewType(24); | ||
let _ = n; | ||
let _ = n; | ||
|
||
let t = (4, 5); | ||
let _ = t; | ||
let _ = t; | ||
|
||
let t2 = (6, (7, 8)); | ||
let (_x, _) = t2; | ||
|
||
let f = Foo { x: 3, y: 7 }; | ||
let _ = f; | ||
} |
This file contains hidden or 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,27 @@ | ||
// run-rustfix | ||
|
||
#![warn(clippy::small_underscore_scope)] | ||
|
||
struct NewType(u8); | ||
|
||
#[allow(dead_code)] | ||
struct Foo { | ||
x: u8, | ||
y: u8, | ||
} | ||
|
||
fn main() { | ||
let n = NewType(24); | ||
let NewType(_) = n; | ||
let NewType(..) = n; | ||
|
||
let t = (4, 5); | ||
let (_, _) = t; | ||
let (..) = t; | ||
|
||
let t2 = (6, (7, 8)); | ||
let (_x, (_, _)) = t2; | ||
|
||
let f = Foo { x: 3, y: 7 }; | ||
let Foo { .. } = f; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a test for You'll probably need to add an |
||
} |
This file contains hidden or 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 @@ | ||
error: this wildcard binding could have a wider scope | ||
--> $DIR/small_underscore_scope.rs:15:9 | ||
| | ||
LL | let NewType(_) = n; | ||
| ^^^^^^^^^^ help: try: `_` | ||
| | ||
= note: `-D clippy::small-underscore-scope` implied by `-D warnings` | ||
|
||
error: this wildcard binding could have a wider scope | ||
--> $DIR/small_underscore_scope.rs:16:9 | ||
| | ||
LL | let NewType(..) = n; | ||
| ^^^^^^^^^^^ help: try: `_` | ||
|
||
error: this wildcard binding could have a wider scope | ||
--> $DIR/small_underscore_scope.rs:19:9 | ||
| | ||
LL | let (_, _) = t; | ||
| ^^^^^^ help: try: `_` | ||
|
||
error: this wildcard binding could have a wider scope | ||
--> $DIR/small_underscore_scope.rs:20:9 | ||
| | ||
LL | let (..) = t; | ||
| ^^^^ help: try: `_` | ||
|
||
error: this wildcard binding could have a wider scope | ||
--> $DIR/small_underscore_scope.rs:23:14 | ||
| | ||
LL | let (_x, (_, _)) = t2; | ||
| ^^^^^^ help: try: `_` | ||
|
||
error: this wildcard binding could have a wider scope | ||
--> $DIR/small_underscore_scope.rs:26:9 | ||
| | ||
LL | let Foo { .. } = f; | ||
| ^^^^^^^^^^ help: try: `_` | ||
|
||
error: aborting due to 6 previous errors | ||
|
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.
I don't think this has to be
ignore
, it should work as a doctest already.