-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add lint long_variable_names
#14818
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
JurrianFahner
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
JurrianFahner:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add lint long_variable_names
#14818
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
use crate::rustc_lint::LintContext; | ||
use crate::rustc_span::Pos; | ||
use clippy_config::Conf; | ||
use clippy_utils::diagnostics::span_lint_and_help; | ||
use rustc_hir::{Pat, PatKind}; | ||
use rustc_lint::{LateContext, LateLintPass}; | ||
use rustc_session::impl_lint_pass; | ||
|
||
declare_clippy_lint! { | ||
/// ### What it does | ||
/// Checks for variable that exceeds a configurable number characters. | ||
/// | ||
/// ### Why is this bad? | ||
/// Long variable names can make code harder to read and thus to maintain. | ||
/// | ||
/// ### Example | ||
/// ```no_run | ||
/// let ferris_fixes_more_bugs_than_your_entire_devops_team_does = "content of a string"; | ||
/// ``` | ||
/// Use instead: | ||
/// ```no_run | ||
/// let ferris_fixes_more_bugs = "content of a string"; | ||
/// ``` | ||
#[clippy::version = "1.88.0"] | ||
pub LONG_VARIABLE_NAMES, | ||
style, | ||
"usage of a long variable" | ||
} | ||
pub struct LongVariableNames { | ||
pub max_variable_name_length: u32, | ||
} | ||
|
||
impl LongVariableNames { | ||
pub fn new(conf: &'static Conf) -> Self { | ||
Self { | ||
max_variable_name_length: conf.max_variable_name_length, | ||
} | ||
} | ||
} | ||
|
||
impl_lint_pass!(LongVariableNames => [LONG_VARIABLE_NAMES]); | ||
|
||
impl<'tcx> LateLintPass<'tcx> for LongVariableNames { | ||
fn check_pat(&mut self, cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>) { | ||
if let PatKind::Binding(.., ident, _) = pat.kind { | ||
let length_bytes = if ident.span.from_expansion() { | ||
// since the span can't be calculated, we can't lint this | ||
// so we just return | ||
return; | ||
} else { | ||
let higher_bound = cx.sess().source_map().lookup_char_pos(ident.span.hi()).col; | ||
let lower_bound = cx.sess().source_map().lookup_char_pos(ident.span.lo()).col; | ||
(higher_bound - lower_bound).to_u32() + 1 | ||
}; | ||
if length_bytes > self.max_variable_name_length { | ||
let variable_name_length = u32::try_from(ident.name.to_ident_string().chars().count()) | ||
.expect("the variable name length exceeds u32::MAX"); | ||
JurrianFahner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if variable_name_length > self.max_variable_name_length { | ||
let length_diff = variable_name_length - self.max_variable_name_length; | ||
|
||
span_lint_and_help( | ||
cx, | ||
LONG_VARIABLE_NAMES, | ||
ident.span, | ||
format!( | ||
"variable name is longer than the configured `max-variable-name-length` of ({} characters)", | ||
self.max_variable_name_length | ||
), | ||
None, | ||
format!("reduce the length of the variable name with at least {length_diff} characters"), | ||
); | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or 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 |
---|---|---|
@@ -1,2 +1,5 @@ | ||
# default config for tests, overrides clippy.toml at the project root | ||
lint-commented-code = false | ||
|
||
# override the default length of variables to test the configuration | ||
max-variable-name-length = 50 |
This file contains hidden or 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 hidden or 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,15 @@ | ||
#![warn(clippy::long_variable_names)] | ||
|
||
fn a_function(ferris_singlehandedly_refactored_the_monolith_while_juggling_crates_and_lifetimes: &str) { | ||
//~^ long_variable_names | ||
} | ||
|
||
fn another_function(just_a_short_name: &str) { | ||
// should not cause a problem | ||
} | ||
|
||
fn main() { | ||
// `ferris_singlehandedly_refactored_the_monolith_while_juggling_crates_and_lifetimes` is too long | ||
let ferris_singlehandedly_refactored_the_monolith_while_juggling_crates_and_lifetimes = "very long indeed"; | ||
//~^ long_variable_names | ||
} |
This file contains hidden or 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,20 @@ | ||
error: variable name is longer than the configured `max-variable-name-length` of (50 characters) | ||
--> tests/ui/long_variable_names.rs:3:15 | ||
| | ||
LL | fn a_function(ferris_singlehandedly_refactored_the_monolith_while_juggling_crates_and_lifetimes: &str) { | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: reduce the length of the variable name with at least 31 characters | ||
= note: `-D clippy::long-variable-names` implied by `-D warnings` | ||
= help: to override `-D warnings` add `#[allow(clippy::long_variable_names)]` | ||
|
||
error: variable name is longer than the configured `max-variable-name-length` of (50 characters) | ||
--> tests/ui/long_variable_names.rs:13:9 | ||
| | ||
LL | let ferris_singlehandedly_refactored_the_monolith_while_juggling_crates_and_lifetimes = "very long indeed"; | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| | ||
= help: reduce the length of the variable name with at least 31 characters | ||
|
||
error: aborting due to 2 previous errors | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.