Skip to content

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
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -1056,6 +1056,7 @@ All notable changes to this project will be documented in this file.
[`single_match`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match
[`single_match_else`]: https://rust-lang.github.io/rust-clippy/master/index.html#single_match_else
[`slow_vector_initialization`]: https://rust-lang.github.io/rust-clippy/master/index.html#slow_vector_initialization
[`small_underscore_scope`]: https://rust-lang.github.io/rust-clippy/master/index.html#small_underscore_scope
[`str_to_string`]: https://rust-lang.github.io/rust-clippy/master/index.html#str_to_string
[`string_add`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add
[`string_add_assign`]: https://rust-lang.github.io/rust-clippy/master/index.html#string_add_assign
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

A collection of lints to catch common mistakes and improve your [Rust](https://github.com/rust-lang/rust) code.

[There are 299 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)
[There are 300 lints included in this crate!](https://rust-lang.github.io/rust-clippy/master/index.html)

We have a bunch of lint categories to allow you to choose how much Clippy is supposed to ~~annoy~~ help you:

Expand Down
3 changes: 3 additions & 0 deletions clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ pub mod returns;
pub mod serde_api;
pub mod shadow;
pub mod slow_vector_initialization;
pub mod small_underscore_scope;
pub mod strings;
pub mod suspicious_trait_impl;
pub mod swap;
Expand Down Expand Up @@ -574,6 +575,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
reg.register_late_lint_pass(box missing_const_for_fn::MissingConstForFn);
reg.register_late_lint_pass(box transmuting_null::TransmutingNull);
reg.register_late_lint_pass(box path_buf_push_overwrite::PathBufPushOverwrite);
reg.register_early_lint_pass(box small_underscore_scope::SmallUnderscoreScope);

reg.register_lint_group("clippy::restriction", Some("clippy_restriction"), vec![
arithmetic::FLOAT_ARITHMETIC,
Expand Down Expand Up @@ -634,6 +636,7 @@ pub fn register_plugins(reg: &mut rustc_plugin::Registry<'_>, conf: &Conf) {
non_expressive_names::SIMILAR_NAMES,
replace_consts::REPLACE_CONSTS,
shadow::SHADOW_UNRELATED,
small_underscore_scope::SMALL_UNDERSCORE_SCOPE,
strings::STRING_ADD_ASSIGN,
types::CAST_POSSIBLE_TRUNCATION,
types::CAST_POSSIBLE_WRAP,
Expand Down
69 changes: 69 additions & 0 deletions clippy_lints/src/small_underscore_scope.rs
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
Copy link
Member

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.

/// // 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,
)
}
27 changes: 27 additions & 0 deletions tests/ui/small_underscore_scope.fixed
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;
}
27 changes: 27 additions & 0 deletions tests/ui/small_underscore_scope.rs
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;
Copy link
Member

@phansch phansch Apr 21, 2019

Choose a reason for hiding this comment

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

Could you add a test for println!("foo");? It currently triggers on macro calls like here.

You'll probably need to add an in_external_macro check and return early in your lint code.

}
40 changes: 40 additions & 0 deletions tests/ui/small_underscore_scope.stderr
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