-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Add new lint upper_case_acronyms
#6475
Merged
Merged
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 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
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,93 @@ | ||
use crate::utils::span_lint_and_sugg; | ||
use if_chain::if_chain; | ||
use itertools::Itertools; | ||
use rustc_ast::ast::{Item, ItemKind, Variant}; | ||
use rustc_errors::Applicability; | ||
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext}; | ||
use rustc_middle::lint::in_external_macro; | ||
use rustc_session::{declare_lint_pass, declare_tool_lint}; | ||
use rustc_span::symbol::Ident; | ||
|
||
declare_clippy_lint! { | ||
/// **What it does:** Checks for camel case name containing a capitalized acronym. | ||
/// | ||
/// **Why is this bad?** In CamelCase, acronyms count as one word. | ||
/// See [naming conventions](https://rust-lang.github.io/api-guidelines/naming.html#casing-conforms-to-rfc-430-c-case) | ||
/// for more. | ||
/// | ||
/// **Known problems:** When two acronyms are contiguous, the lint can't tell where | ||
/// the first acronym ends and the second starts, so it suggests to lowercase all of | ||
/// the letters in the second acronym. | ||
/// | ||
/// **Example:** | ||
/// | ||
/// ```rust | ||
/// struct HTTPResponse; | ||
/// ``` | ||
/// Use instead: | ||
/// ```rust | ||
/// struct HttpResponse; | ||
/// ``` | ||
pub UPPER_CASE_ACRONYMS, | ||
style, | ||
"capitalized acronyms are against the naming convention" | ||
} | ||
|
||
declare_lint_pass!(UpperCaseAcronyms => [UPPER_CASE_ACRONYMS]); | ||
|
||
fn correct_ident(ident: &str) -> String { | ||
let ident = ident.chars().rev().collect::<String>(); | ||
let fragments = ident | ||
.split_inclusive(|x: char| !x.is_ascii_lowercase()) | ||
.rev() | ||
.map(|x| x.chars().rev().collect::<String>()); | ||
|
||
let mut ident = fragments.clone().next().unwrap(); | ||
for (ref prev, ref curr) in fragments.tuple_windows() { | ||
if [prev, curr] | ||
.iter() | ||
.all(|s| s.len() == 1 && s.chars().next().unwrap().is_ascii_uppercase()) | ||
{ | ||
ident.push_str(&curr.to_ascii_lowercase()); | ||
} else { | ||
ident.push_str(curr); | ||
} | ||
} | ||
ident | ||
} | ||
|
||
fn check_ident(cx: &EarlyContext<'_>, ident: &Ident) { | ||
let span = ident.span; | ||
let ident = &ident.as_str(); | ||
let corrected = correct_ident(ident); | ||
if ident != &corrected { | ||
span_lint_and_sugg( | ||
cx, | ||
UPPER_CASE_ACRONYMS, | ||
span, | ||
&format!("name `{}` contains a capitalized acronym", ident), | ||
"consider making the acronym lowercase, except the initial letter", | ||
corrected, | ||
Applicability::MaybeIncorrect, | ||
) | ||
} | ||
} | ||
|
||
impl EarlyLintPass for UpperCaseAcronyms { | ||
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &Item) { | ||
if_chain! { | ||
if !in_external_macro(cx.sess(), it.span); | ||
if matches!( | ||
it.kind, | ||
ItemKind::TyAlias(..) | ItemKind::Enum(..) | ItemKind::Struct(..) | ItemKind::Trait(..) | ||
); | ||
then { | ||
check_ident(cx, &it.ident); | ||
} | ||
} | ||
} | ||
|
||
fn check_variant(&mut self, cx: &EarlyContext<'_>, v: &Variant) { | ||
check_ident(cx, &v.ident); | ||
} | ||
} |
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
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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Uppercase is usually written as one word. Like in the
char::to_uppercase
methodThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's
non-upper-case-globals
lint in rustc and I suppose it's reasonable to make the name of this lint similar to that. (It seems they wanted to go consistent withnon-camel-case-types
andnon_snake_case
when naming the rustc lint)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Well, I find that inconsistent. But lets be consistently inconsistent and keep the name as it is 👍