Skip to content

Commit

Permalink
Auto merge of #7166 - TaKO8Ki:refactor_misc_early_module, r=llogiq
Browse files Browse the repository at this point in the history
Refactor: arrange lints in misc_early module

This PR arranges misc_early lints so that they can be accessed more easily.
Basically, I refactored them following the instruction described in #6680.

cc: `@Y-Nak,` `@flip1995,` `@magurotuna`

changelog: Move lints in misc_early module into their own modules.
  • Loading branch information
bors committed May 6, 2021
2 parents 9dd8705 + 3fbb060 commit 7191675
Show file tree
Hide file tree
Showing 12 changed files with 636 additions and 571 deletions.
569 changes: 0 additions & 569 deletions clippy_lints/src/misc_early.rs

This file was deleted.

19 changes: 19 additions & 0 deletions clippy_lints/src/misc_early/builtin_type_shadow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use clippy_utils::diagnostics::span_lint;
use rustc_ast::ast::{GenericParam, GenericParamKind};
use rustc_hir::PrimTy;
use rustc_lint::EarlyContext;

use super::BUILTIN_TYPE_SHADOW;

pub(super) fn check(cx: &EarlyContext<'_>, param: &GenericParam) {
if let GenericParamKind::Type { .. } = param.kind {
if let Some(prim_ty) = PrimTy::from_name(param.ident.name) {
span_lint(
cx,
BUILTIN_TYPE_SHADOW,
param.ident.span,
&format!("this generic shadows the built-in type `{}`", prim_ty.name()),
);
}
}
}
23 changes: 23 additions & 0 deletions clippy_lints/src/misc_early/double_neg.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
use super::MiscEarlyLints;
use clippy_utils::diagnostics::span_lint;
use rustc_ast::ast::{Expr, ExprKind, UnOp};
use rustc_lint::EarlyContext;

use super::DOUBLE_NEG;

pub(super) fn check(cx: &EarlyContext<'_>, expr: &Expr) {
match expr.kind {
ExprKind::Unary(UnOp::Neg, ref inner) => {
if let ExprKind::Unary(UnOp::Neg, _) = inner.kind {
span_lint(
cx,
DOUBLE_NEG,
expr.span,
"`--x` could be misinterpreted as pre-decrement by C programmers, is usually a no-op",
);
}
},
ExprKind::Lit(ref lit) => MiscEarlyLints::check_lit(cx, lit),
_ => (),
}
}
34 changes: 34 additions & 0 deletions clippy_lints/src/misc_early/mixed_case_hex_literals.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use clippy_utils::diagnostics::span_lint;
use rustc_ast::ast::Lit;
use rustc_lint::EarlyContext;

use super::MIXED_CASE_HEX_LITERALS;

pub(super) fn check(cx: &EarlyContext<'_>, lit: &Lit, suffix: &str, lit_snip: &str) {
let maybe_last_sep_idx = if let Some(val) = lit_snip.len().checked_sub(suffix.len() + 1) {
val
} else {
return; // It's useless so shouldn't lint.
};
if maybe_last_sep_idx <= 2 {
// It's meaningless or causes range error.
return;
}
let mut seen = (false, false);
for ch in lit_snip.as_bytes()[2..=maybe_last_sep_idx].iter() {
match ch {
b'a'..=b'f' => seen.0 = true,
b'A'..=b'F' => seen.1 = true,
_ => {},
}
if seen.0 && seen.1 {
span_lint(
cx,
MIXED_CASE_HEX_LITERALS,
lit.span,
"inconsistent casing in hexadecimal literal",
);
break;
}
}
}
Loading

0 comments on commit 7191675

Please sign in to comment.