Warns if variable name is composed only of underscores and digits.
This commit is contained in:
parent
47be692723
commit
9d01468bc7
3 changed files with 57 additions and 1 deletions
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue