forked from rust-lang/rust
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of rust-lang#11013 - Centri3:redundant_rest_pattern, r=gir…
…affate New lint [`redundant_at_rest_pattern`] Closes rust-lang#11011 It's always a great feeling when a new lint triggers on clippy itself 😄 changelog: New lint [`redundant_at_rest_pattern`]
- Loading branch information
Showing
13 changed files
with
173 additions
and
14 deletions.
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use rustc_ast::{Pat, PatKind}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
|
||
use super::REDUNDANT_AT_REST_PATTERN; | ||
|
||
pub(super) fn check(cx: &EarlyContext<'_>, pat: &Pat) { | ||
if !in_external_macro(cx.sess(), pat.span) | ||
&& let PatKind::Slice(slice) = &pat.kind | ||
&& let [one] = &**slice | ||
&& let PatKind::Ident(annotation, ident, Some(rest)) = &one.kind | ||
&& let PatKind::Rest = rest.kind | ||
{ | ||
span_lint_and_sugg( | ||
cx, | ||
REDUNDANT_AT_REST_PATTERN, | ||
pat.span, | ||
"using a rest pattern to bind an entire slice to a local", | ||
"this is better represented with just the binding", | ||
format!("{}{ident}", annotation.prefix_str()), | ||
Applicability::MachineApplicable, | ||
); | ||
} | ||
} |
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
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
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,27 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow(irrefutable_let_patterns, unused)] | ||
#![warn(clippy::redundant_at_rest_pattern)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
fn main() { | ||
if let a = [()] {} | ||
if let ref a = [()] {} | ||
if let mut a = [()] {} | ||
if let ref mut a = [()] {} | ||
let v = vec![()]; | ||
if let a = &*v {} | ||
let s = &[()]; | ||
if let a = s {} | ||
// Don't lint | ||
if let [..] = &*v {} | ||
if let [a] = &*v {} | ||
if let [()] = &*v {} | ||
if let [first, rest @ ..] = &*v {} | ||
if let a = [()] {} | ||
external! { | ||
if let [a @ ..] = [()] {} | ||
} | ||
} |
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,27 @@ | ||
//@run-rustfix | ||
//@aux-build:proc_macros.rs:proc-macro | ||
#![allow(irrefutable_let_patterns, unused)] | ||
#![warn(clippy::redundant_at_rest_pattern)] | ||
|
||
#[macro_use] | ||
extern crate proc_macros; | ||
|
||
fn main() { | ||
if let [a @ ..] = [()] {} | ||
if let [ref a @ ..] = [()] {} | ||
if let [mut a @ ..] = [()] {} | ||
if let [ref mut a @ ..] = [()] {} | ||
let v = vec![()]; | ||
if let [a @ ..] = &*v {} | ||
let s = &[()]; | ||
if let [a @ ..] = s {} | ||
// Don't lint | ||
if let [..] = &*v {} | ||
if let [a] = &*v {} | ||
if let [()] = &*v {} | ||
if let [first, rest @ ..] = &*v {} | ||
if let a = [()] {} | ||
external! { | ||
if let [a @ ..] = [()] {} | ||
} | ||
} |
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 @@ | ||
error: using a rest pattern to bind an entire slice to a local | ||
--> $DIR/redundant_at_rest_pattern.rs:10:12 | ||
| | ||
LL | if let [a @ ..] = [()] {} | ||
| ^^^^^^^^ help: this is better represented with just the binding: `a` | ||
| | ||
= note: `-D clippy::redundant-at-rest-pattern` implied by `-D warnings` | ||
|
||
error: using a rest pattern to bind an entire slice to a local | ||
--> $DIR/redundant_at_rest_pattern.rs:11:12 | ||
| | ||
LL | if let [ref a @ ..] = [()] {} | ||
| ^^^^^^^^^^^^ help: this is better represented with just the binding: `ref a` | ||
|
||
error: using a rest pattern to bind an entire slice to a local | ||
--> $DIR/redundant_at_rest_pattern.rs:12:12 | ||
| | ||
LL | if let [mut a @ ..] = [()] {} | ||
| ^^^^^^^^^^^^ help: this is better represented with just the binding: `mut a` | ||
|
||
error: using a rest pattern to bind an entire slice to a local | ||
--> $DIR/redundant_at_rest_pattern.rs:13:12 | ||
| | ||
LL | if let [ref mut a @ ..] = [()] {} | ||
| ^^^^^^^^^^^^^^^^ help: this is better represented with just the binding: `ref mut a` | ||
|
||
error: using a rest pattern to bind an entire slice to a local | ||
--> $DIR/redundant_at_rest_pattern.rs:15:12 | ||
| | ||
LL | if let [a @ ..] = &*v {} | ||
| ^^^^^^^^ help: this is better represented with just the binding: `a` | ||
|
||
error: using a rest pattern to bind an entire slice to a local | ||
--> $DIR/redundant_at_rest_pattern.rs:17:12 | ||
| | ||
LL | if let [a @ ..] = s {} | ||
| ^^^^^^^^ help: this is better represented with just the binding: `a` | ||
|
||
error: aborting due to 6 previous errors | ||
|