Warns if variable name is composed only of underscores and digits.
This commit is contained in:
parent
47be692723
commit
9d01468bc7
@ -42,13 +42,33 @@ declare_lint! {
|
||||
"too many single character bindings"
|
||||
}
|
||||
|
||||
/// **What it does:** Checks if you have variables whose name consists of just
|
||||
/// underscores and digits.
|
||||
///
|
||||
/// **Why is this bad?** It's hard to memorize what a variable means without a
|
||||
/// descriptive name.
|
||||
///
|
||||
/// **Known problems:** None?
|
||||
///
|
||||
/// **Example:**
|
||||
/// ```rust
|
||||
/// let _1 = 1;
|
||||
/// let ___1 = 1;
|
||||
/// let __1___2 = 11;
|
||||
/// ```
|
||||
declare_lint! {
|
||||
pub JUST_UNDERSCORES_AND_DIGITS,
|
||||
Warn,
|
||||
"unclear name"
|
||||
}
|
||||
|
||||
pub struct NonExpressiveNames {
|
||||
pub single_char_binding_names_threshold: u64,
|
||||
}
|
||||
|
||||
impl LintPass for NonExpressiveNames {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(SIMILAR_NAMES, MANY_SINGLE_CHAR_NAMES)
|
||||
lint_array!(SIMILAR_NAMES, MANY_SINGLE_CHAR_NAMES, JUST_UNDERSCORES_AND_DIGITS)
|
||||
}
|
||||
}
|
||||
|
||||
@ -133,6 +153,15 @@ impl<'a, 'tcx, 'b> SimilarNamesNameVisitor<'a, 'tcx, 'b> {
|
||||
if interned_name.chars().any(char::is_uppercase) {
|
||||
return;
|
||||
}
|
||||
if interned_name.chars().all(|c| c.is_digit(10) || c == '_') {
|
||||
span_lint(
|
||||
self.0.cx,
|
||||
JUST_UNDERSCORES_AND_NUMBERS,
|
||||
span,
|
||||
"binding whose name is just underscores and digits",
|
||||
);
|
||||
return;
|
||||
}
|
||||
let count = interned_name.chars().count();
|
||||
if count < 3 {
|
||||
if count == 1 {
|
||||
|
@ -134,3 +134,10 @@ fn bla() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn underscores_and_numbers() {
|
||||
let _1 = 1; //~ERROR Consider a more descriptive name
|
||||
let ____1 = 1; //~ERROR Consider a more descriptive name
|
||||
let __1___2 = 12; //~ERROR Consider a more descriptive name
|
||||
let _1_ok= 1;
|
||||
}
|
@ -129,3 +129,23 @@ error: 5th binding whose name is just one char
|
||||
129 | e => panic!(),
|
||||
| ^
|
||||
|
||||
error: binding whose name is just underscores and digits
|
||||
--> $DIR/non_expressive_names.rs:139:9
|
||||
|
|
||||
139 | let _1 = 1; //~ERROR Consider a more descriptive name
|
||||
| ^^
|
||||
|
|
||||
= note: `-D just-underscores-and-numbers` implied by `-D warnings`
|
||||
|
||||
error: binding whose name is just underscores and digits
|
||||
--> $DIR/non_expressive_names.rs:140:9
|
||||
|
|
||||
140 | let ____1 = 1; //~ERROR Consider a more descriptive name
|
||||
| ^^^^^
|
||||
|
||||
error: binding whose name is just underscores and digits
|
||||
--> $DIR/non_expressive_names.rs:141:9
|
||||
|
|
||||
141 | let __1___2 = 12; //~ERROR Consider a more descriptive name
|
||||
| ^^^^^^^
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user