Skip to content

Commit

Permalink
Use break api config for upper_case_acronyms
Browse files Browse the repository at this point in the history
  • Loading branch information
camsteffen committed May 26, 2021
1 parent 1ce581d commit 55ccc7a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 14 deletions.
2 changes: 1 addition & 1 deletion clippy_lints/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2020,7 +2020,7 @@ pub fn register_plugins(store: &mut rustc_lint::LintStore, sess: &Session, conf:
store.register_late_pass(move || box enum_variants::EnumVariantNames::new(enum_variant_name_threshold, avoid_breaking_exported_api));
store.register_early_pass(|| box tabs_in_doc_comments::TabsInDocComments);
let upper_case_acronyms_aggressive = conf.upper_case_acronyms_aggressive;
store.register_early_pass(move || box upper_case_acronyms::UpperCaseAcronyms::new(upper_case_acronyms_aggressive));
store.register_late_pass(move || box upper_case_acronyms::UpperCaseAcronyms::new(avoid_breaking_exported_api, upper_case_acronyms_aggressive));
store.register_late_pass(|| box default::Default::default());
store.register_late_pass(|| box unused_self::UnusedSelf);
store.register_late_pass(|| box mutable_debug_assertion::DebugAssertWithMutCall);
Expand Down
32 changes: 19 additions & 13 deletions clippy_lints/src/upper_case_acronyms.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use itertools::Itertools;
use rustc_ast::ast::{Item, ItemKind, VisibilityKind};
use rustc_errors::Applicability;
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
use rustc_hir::{Item, ItemKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_tool_lint, impl_lint_pass};
use rustc_span::symbol::Ident;
Expand Down Expand Up @@ -38,12 +38,14 @@ declare_clippy_lint! {

#[derive(Default)]
pub struct UpperCaseAcronyms {
avoid_breaking_exported_api: bool,
upper_case_acronyms_aggressive: bool,
}

impl UpperCaseAcronyms {
pub fn new(aggressive: bool) -> Self {
pub fn new(avoid_breaking_exported_api: bool, aggressive: bool) -> Self {
Self {
avoid_breaking_exported_api,
upper_case_acronyms_aggressive: aggressive,
}
}
Expand Down Expand Up @@ -72,7 +74,7 @@ fn correct_ident(ident: &str) -> String {
ident
}

fn check_ident(cx: &EarlyContext<'_>, ident: &Ident, be_aggressive: bool) {
fn check_ident(cx: &LateContext<'_>, ident: &Ident, be_aggressive: bool) {
let span = ident.span;
let ident = &ident.as_str();
let corrected = correct_ident(ident);
Expand All @@ -96,23 +98,27 @@ fn check_ident(cx: &EarlyContext<'_>, ident: &Ident, be_aggressive: bool) {
}
}

impl EarlyLintPass for UpperCaseAcronyms {
fn check_item(&mut self, cx: &EarlyContext<'_>, it: &Item) {
impl LateLintPass<'_> for UpperCaseAcronyms {
fn check_item(&mut self, cx: &LateContext<'_>, it: &Item<'_>) {
// do not lint public items or in macros
if !in_external_macro(cx.sess(), it.span) && !matches!(it.vis.kind, VisibilityKind::Public) {
if matches!(
it.kind,
ItemKind::TyAlias(..) | ItemKind::Struct(..) | ItemKind::Trait(..)
) {
if in_external_macro(cx.sess(), it.span)
|| (self.avoid_breaking_exported_api && cx.access_levels.is_exported(it.hir_id()))
{
return;
}
match it.kind {
ItemKind::TyAlias(..) | ItemKind::Struct(..) | ItemKind::Trait(..) => {
check_ident(cx, &it.ident, self.upper_case_acronyms_aggressive);
} else if let ItemKind::Enum(ref enumdef, _) = it.kind {
},
ItemKind::Enum(ref enumdef, _) => {
// check enum variants seperately because again we only want to lint on private enums and
// the fn check_variant does not know about the vis of the enum of its variants
enumdef
.variants
.iter()
.for_each(|variant| check_ident(cx, &variant.ident, self.upper_case_acronyms_aggressive));
}
},
_ => {},
}
}
}

0 comments on commit 55ccc7a

Please sign in to comment.