From 9d01468bc7a3e7311ff6ed4e64395818dc64fee1 Mon Sep 17 00:00:00 2001 From: Alexandru Ene Date: Fri, 3 Nov 2017 01:01:41 +0000 Subject: [PATCH] Warns if variable name is composed only of underscores and digits. --- clippy_lints/src/non_expressive_names.rs | 31 +++++++++++++++++++++++- tests/ui/non_expressive_names.rs | 7 ++++++ tests/ui/non_expressive_names.stderr | 20 +++++++++++++++ 3 files changed, 57 insertions(+), 1 deletion(-) diff --git a/clippy_lints/src/non_expressive_names.rs b/clippy_lints/src/non_expressive_names.rs index 6cbeea8214d..e4d7049bf01 100644 --- a/clippy_lints/src/non_expressive_names.rs +++ b/clippy_lints/src/non_expressive_names.rs @@ -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 { diff --git a/tests/ui/non_expressive_names.rs b/tests/ui/non_expressive_names.rs index 9eb3e5a82a7..16a035ca024 100644 --- a/tests/ui/non_expressive_names.rs +++ b/tests/ui/non_expressive_names.rs @@ -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; +} \ No newline at end of file diff --git a/tests/ui/non_expressive_names.stderr b/tests/ui/non_expressive_names.stderr index 014d4599271..7141c97dd1a 100644 --- a/tests/ui/non_expressive_names.stderr +++ b/tests/ui/non_expressive_names.stderr @@ -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 + | ^^^^^^^ +