-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
198 additions
and
7 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
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,80 @@ | ||
use clippy_utils::diagnostics::span_lint_and_sugg; | ||
use clippy_utils::in_macro; | ||
use clippy_utils::source::snippet_with_applicability; | ||
use rustc_errors::Applicability; | ||
use rustc_hir::{Expr, ExprKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use std::borrow::Cow; | ||
use std::cmp::Reverse; | ||
use std::collections::BinaryHeap; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for tuple structs initialized with field syntax. | ||
/// It will however not lint if a base initializer is present. | ||
/// The lint will also ignore code in macros. | ||
/// | ||
/// ### Why is this bad? | ||
/// This may be confusing to the uninitiated and adds no | ||
/// benefit as opposed to tuple initializers | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// struct TupleStruct(u8, u16); | ||
/// | ||
/// let _ = TupleStruct { | ||
/// 0: 1, | ||
/// 1: 23, | ||
/// }; | ||
/// | ||
/// // should be written as | ||
/// let base = TupleStruct(1, 23); | ||
/// | ||
/// // This is OK however | ||
/// let _ = TupleStruct { 0: 42, ..base }; | ||
/// ``` | ||
#[clippy::version = "1.59.0"] | ||
pub NUMBERED_FIELDS, | ||
style, | ||
"numbered fields in tuple struct initializer" | ||
} | ||
|
||
declare_lint_pass!(NumberedFields => [NUMBERED_FIELDS]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for NumberedFields { | ||
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &'tcx Expr<'_>) { | ||
if let ExprKind::Struct(path, fields, None) = e.kind { | ||
if !fields.is_empty() | ||
&& !in_macro(e.span) | ||
&& fields | ||
.iter() | ||
.all(|f| f.ident.as_str().as_bytes().iter().all(u8::is_ascii_digit)) | ||
{ | ||
let expr_spans = fields | ||
.iter() | ||
.map(|f| (Reverse(f.ident.as_str().parse::<usize>().unwrap()), f.expr.span)) | ||
.collect::<BinaryHeap<_>>(); | ||
let mut appl = Applicability::MachineApplicable; | ||
let snippet = format!( | ||
"{}({})", | ||
snippet_with_applicability(cx, path.span(), "..", &mut appl), | ||
expr_spans | ||
.into_iter_sorted() | ||
.map(|(_, span)| snippet_with_applicability(cx, span, "..", &mut appl)) | ||
.intersperse(Cow::Borrowed(", ")) | ||
.collect::<String>() | ||
); | ||
span_lint_and_sugg( | ||
cx, | ||
NUMBERED_FIELDS, | ||
e.span, | ||
"used a field initializer for a tuple struct", | ||
"try this instead", | ||
snippet, | ||
appl, | ||
); | ||
} | ||
} | ||
} | ||
} |
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,33 @@ | ||
//run-rustfix | ||
#![warn(clippy::numbered_fields)] | ||
|
||
#[derive(Default)] | ||
struct TupleStruct(u32, u32, u8); | ||
|
||
// This shouldn't lint because it's in a macro | ||
macro_rules! tuple_struct_init { | ||
() => { | ||
TupleStruct { 0: 0, 1: 1, 2: 2 } | ||
}; | ||
} | ||
|
||
fn main() { | ||
let tuple_struct = TupleStruct::default(); | ||
|
||
// This should lint | ||
let _ = TupleStruct(1u32, 42, 23u8); | ||
|
||
// This should also lint and order the fields correctly | ||
let _ = TupleStruct(1u32, 3u32, 2u8); | ||
|
||
// Ok because of default initializer | ||
let _ = TupleStruct { 0: 42, ..tuple_struct }; | ||
|
||
let _ = TupleStruct { | ||
1: 23, | ||
..TupleStruct::default() | ||
}; | ||
|
||
// Ok because it's in macro | ||
let _ = tuple_struct_init!(); | ||
} |
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,41 @@ | ||
//run-rustfix | ||
#![warn(clippy::numbered_fields)] | ||
|
||
#[derive(Default)] | ||
struct TupleStruct(u32, u32, u8); | ||
|
||
// This shouldn't lint because it's in a macro | ||
macro_rules! tuple_struct_init { | ||
() => { | ||
TupleStruct { 0: 0, 1: 1, 2: 2 } | ||
}; | ||
} | ||
|
||
fn main() { | ||
let tuple_struct = TupleStruct::default(); | ||
|
||
// This should lint | ||
let _ = TupleStruct { | ||
0: 1u32, | ||
1: 42, | ||
2: 23u8, | ||
}; | ||
|
||
// This should also lint and order the fields correctly | ||
let _ = TupleStruct { | ||
0: 1u32, | ||
2: 2u8, | ||
1: 3u32, | ||
}; | ||
|
||
// Ok because of default initializer | ||
let _ = TupleStruct { 0: 42, ..tuple_struct }; | ||
|
||
let _ = TupleStruct { | ||
1: 23, | ||
..TupleStruct::default() | ||
}; | ||
|
||
// Ok because it's in macro | ||
let _ = tuple_struct_init!(); | ||
} |
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 @@ | ||
error: used a field initializer for a tuple struct | ||
--> $DIR/numbered_fields.rs:18:13 | ||
| | ||
LL | let _ = TupleStruct { | ||
| _____________^ | ||
LL | | 0: 1u32, | ||
LL | | 1: 42, | ||
LL | | 2: 23u8, | ||
LL | | }; | ||
| |_____^ help: try this instead: `TupleStruct(1u32, 42, 23u8)` | ||
| | ||
= note: `-D clippy::numbered-fields` implied by `-D warnings` | ||
|
||
error: used a field initializer for a tuple struct | ||
--> $DIR/numbered_fields.rs:25:13 | ||
| | ||
LL | let _ = TupleStruct { | ||
| _____________^ | ||
LL | | 0: 1u32, | ||
LL | | 2: 2u8, | ||
LL | | 1: 3u32, | ||
LL | | }; | ||
| |_____^ help: try this instead: `TupleStruct(1u32, 3u32, 2u8)` | ||
|
||
error: aborting due to 2 previous errors | ||
|