-
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
9 changed files
with
251 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
use clippy_utils::{diagnostics::span_lint, source::snippet}; | ||
use itertools::Itertools; | ||
use rustc_data_structures::fx::FxHashSet; | ||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_tool_lint, impl_lint_pass}; | ||
use rustc_span::symbol::Ident; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for idents which comprise of a single letter. | ||
/// | ||
/// Note: This lint can be very noisy when enabled; it even lints generics! it may be desirable | ||
/// to only enable it temporarily. | ||
/// | ||
/// ### Why is this bad? | ||
/// In many cases it's not, but at times it can severely hinder readability. Some codebases may | ||
/// wish to disallow this to improve readability. | ||
/// | ||
/// ### Example | ||
/// ```rust | ||
/// for i in collection { | ||
/// let x = i.x; | ||
/// } | ||
/// ``` | ||
#[clippy::version = "1.72.0"] | ||
pub SINGLE_LETTER_IDENTS, | ||
restriction, | ||
"disallows idents that can be represented as a char" | ||
} | ||
impl_lint_pass!(SingleLetterIdents => [SINGLE_LETTER_IDENTS]); | ||
|
||
#[derive(Clone)] | ||
pub struct SingleLetterIdents { | ||
pub allowed_idents: FxHashSet<char>, | ||
} | ||
|
||
impl EarlyLintPass for SingleLetterIdents { | ||
fn check_ident(&mut self, cx: &EarlyContext<'_>, ident: Ident) { | ||
let str = ident.name.as_str(); | ||
let chars = str.chars(); | ||
if let [char, rest @ ..] = chars.collect_vec().as_slice() | ||
&& rest.is_empty() | ||
&& self.allowed_idents.get(char).is_none() | ||
&& !in_external_macro(cx.sess(), ident.span) | ||
// Ignore proc macros. Let's implement `WithSearchPat` for early lints someday :) | ||
&& snippet(cx, ident.span, str) == str | ||
{ | ||
span_lint(cx, SINGLE_LETTER_IDENTS, ident.span, "this ident comprises of a single letter"); | ||
} | ||
} | ||
} |
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,59 @@ | ||
//@aux-build:proc_macros.rs | ||
#![allow(nonstandard_style, unused)] | ||
#![warn(clippy::single_letter_idents)] | ||
|
||
extern crate proc_macros; | ||
use proc_macros::external; | ||
use proc_macros::with_span; | ||
|
||
struct A { | ||
a: u32, | ||
i: u32, | ||
A: u32, | ||
I: u32, | ||
} | ||
|
||
struct B(u32); | ||
|
||
struct i; | ||
|
||
enum C { | ||
D, | ||
E, | ||
F, | ||
j, | ||
} | ||
|
||
struct Vec4 { | ||
x: u32, | ||
y: u32, | ||
z: u32, | ||
w: u32, | ||
} | ||
|
||
struct AA<T, E>(T, E); | ||
|
||
fn main() { | ||
// Allowed idents | ||
let w = 1; | ||
// Ok, not this one | ||
// let i = 1; | ||
let j = 1; | ||
let n = 1; | ||
let x = 1; | ||
let y = 1; | ||
let z = 1; | ||
|
||
for j in 0..1000 {} | ||
|
||
// Do not lint code from external macros | ||
external! { for j in 0..1000 {} } | ||
// Do not lint code from procedural macros | ||
with_span! { | ||
span | ||
for j in 0..1000 {} | ||
} | ||
} | ||
|
||
fn b() {} | ||
fn owo() {} |
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,100 @@ | ||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:9:8 | ||
| | ||
LL | struct A { | ||
| ^ | ||
| | ||
= note: `-D clippy::single-letter-idents` implied by `-D warnings` | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:10:5 | ||
| | ||
LL | a: u32, | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:12:5 | ||
| | ||
LL | A: u32, | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:13:5 | ||
| | ||
LL | I: u32, | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:16:8 | ||
| | ||
LL | struct B(u32); | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:20:6 | ||
| | ||
LL | enum C { | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:21:5 | ||
| | ||
LL | D, | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:22:5 | ||
| | ||
LL | E, | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:23:5 | ||
| | ||
LL | F, | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:31:5 | ||
| | ||
LL | w: u32, | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:34:11 | ||
| | ||
LL | struct AA<T, E>(T, E); | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:34:14 | ||
| | ||
LL | struct AA<T, E>(T, E); | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:34:17 | ||
| | ||
LL | struct AA<T, E>(T, E); | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:34:20 | ||
| | ||
LL | struct AA<T, E>(T, E); | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:38:9 | ||
| | ||
LL | let w = 1; | ||
| ^ | ||
|
||
error: this ident comprises of a single letter | ||
--> $DIR/single_letter_idents.rs:58:4 | ||
| | ||
LL | fn b() {} | ||
| ^ | ||
|
||
error: aborting due to 16 previous errors | ||
|