Skip to content

Commit 8fa406b

Browse files
committed
Auto merge of #13069 - Jarcho:misc_small4, r=Manishearth
Misc refactorings part 4 And even more rearrangements to check the HIR tree before other checks. changelog: none
2 parents 87f8a5b + eda45aa commit 8fa406b

19 files changed

+104
-150
lines changed

clippy_lints/src/len_zero.rs

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,9 @@ declare_lint_pass!(LenZero => [LEN_ZERO, LEN_WITHOUT_IS_EMPTY, COMPARISON_TO_EMP
121121

122122
impl<'tcx> LateLintPass<'tcx> for LenZero {
123123
fn check_item(&mut self, cx: &LateContext<'tcx>, item: &'tcx Item<'_>) {
124-
if item.span.from_expansion() {
125-
return;
126-
}
127-
128-
if let ItemKind::Trait(_, _, _, _, trait_items) = item.kind {
124+
if let ItemKind::Trait(_, _, _, _, trait_items) = item.kind
125+
&& !item.span.from_expansion()
126+
{
129127
check_trait_items(cx, item, trait_items);
130128
}
131129
}
@@ -162,17 +160,14 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
162160
}
163161

164162
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
165-
if expr.span.from_expansion() {
166-
return;
167-
}
168-
169163
if let ExprKind::Let(lt) = expr.kind
170-
&& has_is_empty(cx, lt.init)
171164
&& match lt.pat.kind {
172165
PatKind::Slice([], None, []) => true,
173166
PatKind::Lit(lit) if is_empty_string(lit) => true,
174167
_ => false,
175168
}
169+
&& !expr.span.from_expansion()
170+
&& has_is_empty(cx, lt.init)
176171
{
177172
let mut applicability = Applicability::MachineApplicable;
178173

@@ -190,7 +185,9 @@ impl<'tcx> LateLintPass<'tcx> for LenZero {
190185
);
191186
}
192187

193-
if let ExprKind::Binary(Spanned { node: cmp, .. }, left, right) = expr.kind {
188+
if let ExprKind::Binary(Spanned { node: cmp, .. }, left, right) = expr.kind
189+
&& !expr.span.from_expansion()
190+
{
194191
// expr.span might contains parenthesis, see issue #10529
195192
let actual_span = span_without_enclosing_paren(cx, expr.span);
196193
match cmp {

clippy_lints/src/let_if_seq.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,10 @@ declare_lint_pass!(LetIfSeq => [USELESS_LET_IF_SEQ]);
5858

5959
impl<'tcx> LateLintPass<'tcx> for LetIfSeq {
6060
fn check_block(&mut self, cx: &LateContext<'tcx>, block: &'tcx hir::Block<'_>) {
61-
let mut it = block.stmts.iter().peekable();
62-
while let Some(stmt) = it.next() {
63-
if let Some(expr) = it.peek()
64-
&& let hir::StmtKind::Let(local) = stmt.kind
61+
for [stmt, next] in block.stmts.array_windows::<2>() {
62+
if let hir::StmtKind::Let(local) = stmt.kind
6563
&& let hir::PatKind::Binding(mode, canonical_id, ident, None) = local.pat.kind
66-
&& let hir::StmtKind::Expr(if_) = expr.kind
64+
&& let hir::StmtKind::Expr(if_) = next.kind
6765
&& let hir::ExprKind::If(
6866
hir::Expr {
6967
kind: hir::ExprKind::DropTemps(cond),

clippy_lints/src/let_underscore.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,9 +139,9 @@ const SYNC_GUARD_PATHS: [&[&str]; 3] = [
139139
impl<'tcx> LateLintPass<'tcx> for LetUnderscore {
140140
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &LetStmt<'tcx>) {
141141
if matches!(local.source, LocalSource::Normal)
142-
&& !in_external_macro(cx.tcx.sess, local.span)
143142
&& let PatKind::Wild = local.pat.kind
144143
&& let Some(init) = local.init
144+
&& !in_external_macro(cx.tcx.sess, local.span)
145145
{
146146
let init_ty = cx.typeck_results().expr_ty(init);
147147
let contains_sync_guard = init_ty.walk().any(|inner| match inner.unpack() {

clippy_lints/src/let_with_type_underscore.rs

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use clippy_utils::diagnostics::span_lint_and_help;
2-
use clippy_utils::source::snippet;
2+
use clippy_utils::is_from_proc_macro;
33
use rustc_hir::{LetStmt, TyKind};
44
use rustc_lint::{LateContext, LateLintPass};
55
use rustc_middle::lint::in_external_macro;
@@ -25,19 +25,14 @@ declare_clippy_lint! {
2525
}
2626
declare_lint_pass!(UnderscoreTyped => [LET_WITH_TYPE_UNDERSCORE]);
2727

28-
impl LateLintPass<'_> for UnderscoreTyped {
29-
fn check_local(&mut self, cx: &LateContext<'_>, local: &LetStmt<'_>) {
30-
if !in_external_macro(cx.tcx.sess, local.span)
31-
&& let Some(ty) = local.ty // Ensure that it has a type defined
28+
impl<'tcx> LateLintPass<'tcx> for UnderscoreTyped {
29+
fn check_local(&mut self, cx: &LateContext<'tcx>, local: &'tcx LetStmt<'_>) {
30+
if let Some(ty) = local.ty // Ensure that it has a type defined
3231
&& let TyKind::Infer = &ty.kind // that type is '_'
3332
&& local.span.eq_ctxt(ty.span)
33+
&& !in_external_macro(cx.tcx.sess, local.span)
34+
&& !is_from_proc_macro(cx, ty)
3435
{
35-
// NOTE: Using `is_from_proc_macro` on `init` will require that it's initialized,
36-
// this doesn't. Alternatively, `WithSearchPat` can be implemented for `Ty`
37-
if snippet(cx, ty.span, "_").trim() != "_" {
38-
return;
39-
}
40-
4136
span_lint_and_help(
4237
cx,
4338
LET_WITH_TYPE_UNDERSCORE,

clippy_lints/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
#![feature(if_let_guard)]
77
#![feature(is_sorted)]
88
#![feature(iter_intersperse)]
9+
#![feature(iter_partition_in_place)]
910
#![feature(let_chains)]
1011
#![feature(never_type)]
1112
#![feature(rustc_private)]

clippy_lints/src/literal_representation.rs

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -233,11 +233,9 @@ impl_lint_pass!(LiteralDigitGrouping => [
233233

234234
impl EarlyLintPass for LiteralDigitGrouping {
235235
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
236-
if in_external_macro(cx.sess(), expr.span) {
237-
return;
238-
}
239-
240-
if let ExprKind::Lit(lit) = expr.kind {
236+
if let ExprKind::Lit(lit) = expr.kind
237+
&& !in_external_macro(cx.sess(), expr.span)
238+
{
241239
self.check_lit(cx, lit, expr.span);
242240
}
243241
}
@@ -448,11 +446,9 @@ impl_lint_pass!(DecimalLiteralRepresentation => [DECIMAL_LITERAL_REPRESENTATION]
448446

449447
impl EarlyLintPass for DecimalLiteralRepresentation {
450448
fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &Expr) {
451-
if in_external_macro(cx.sess(), expr.span) {
452-
return;
453-
}
454-
455-
if let ExprKind::Lit(lit) = expr.kind {
449+
if let ExprKind::Lit(lit) = expr.kind
450+
&& !in_external_macro(cx.sess(), expr.span)
451+
{
456452
self.check_lit(cx, lit, expr.span);
457453
}
458454
}

clippy_lints/src/manual_bits.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,10 @@ impl_lint_pass!(ManualBits => [MANUAL_BITS]);
5050

5151
impl<'tcx> LateLintPass<'tcx> for ManualBits {
5252
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
53-
if !self.msrv.meets(msrvs::MANUAL_BITS) {
54-
return;
55-
}
56-
5753
if let ExprKind::Binary(bin_op, left_expr, right_expr) = expr.kind
5854
&& let BinOpKind::Mul = &bin_op.node
5955
&& !in_external_macro(cx.sess(), expr.span)
56+
&& self.msrv.meets(msrvs::MANUAL_BITS)
6057
&& let ctxt = expr.span.ctxt()
6158
&& left_expr.span.ctxt() == ctxt
6259
&& right_expr.span.ctxt() == ctxt

clippy_lints/src/manual_float_methods.rs

Lines changed: 13 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,29 +82,26 @@ impl Variant {
8282

8383
impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
8484
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'tcx>) {
85-
if !in_external_macro(cx.sess(), expr.span)
86-
&& (
87-
matches!(cx.tcx.constness(cx.tcx.hir().enclosing_body_owner(expr.hir_id)), Constness::NotConst)
88-
|| cx.tcx.features().declared(sym!(const_float_classify))
89-
) && let ExprKind::Binary(kind, lhs, rhs) = expr.kind
85+
if let ExprKind::Binary(kind, lhs, rhs) = expr.kind
9086
&& let ExprKind::Binary(lhs_kind, lhs_lhs, lhs_rhs) = lhs.kind
9187
&& let ExprKind::Binary(rhs_kind, rhs_lhs, rhs_rhs) = rhs.kind
9288
// Checking all possible scenarios using a function would be a hopeless task, as we have
9389
// 16 possible alignments of constants/operands. For now, let's use `partition`.
94-
&& let (operands, constants) = [lhs_lhs, lhs_rhs, rhs_lhs, rhs_rhs]
95-
.into_iter()
96-
.partition::<Vec<&Expr<'_>>, _>(|i| path_to_local(i).is_some())
97-
&& let [first, second] = &*operands
98-
&& let Some([const_1, const_2]) = constants
99-
.into_iter()
100-
.map(|i| constant(cx, cx.typeck_results(), i))
101-
.collect::<Option<Vec<_>>>()
102-
.as_deref()
90+
&& let mut exprs = [lhs_lhs, lhs_rhs, rhs_lhs, rhs_rhs]
91+
&& exprs.iter_mut().partition_in_place(|i| path_to_local(i).is_some()) == 2
92+
&& !in_external_macro(cx.sess(), expr.span)
93+
&& (
94+
matches!(cx.tcx.constness(cx.tcx.hir().enclosing_body_owner(expr.hir_id)), Constness::NotConst)
95+
|| cx.tcx.features().declared(sym!(const_float_classify))
96+
)
97+
&& let [first, second, const_1, const_2] = exprs
98+
&& let Some(const_1) = constant(cx, cx.typeck_results(), const_1)
99+
&& let Some(const_2) = constant(cx, cx.typeck_results(), const_2)
103100
&& path_to_local(first).is_some_and(|f| path_to_local(second).is_some_and(|s| f == s))
104101
// The actual infinity check, we also allow `NEG_INFINITY` before` INFINITY` just in
105102
// case somebody does that for some reason
106-
&& (is_infinity(const_1) && is_neg_infinity(const_2)
107-
|| is_neg_infinity(const_1) && is_infinity(const_2))
103+
&& (is_infinity(&const_1) && is_neg_infinity(&const_2)
104+
|| is_neg_infinity(&const_1) && is_infinity(&const_2))
108105
&& let Some(local_snippet) = snippet_opt(cx, first.span)
109106
{
110107
let variant = match (kind.node, lhs_kind.node, rhs_kind.node) {

clippy_lints/src/manual_let_else.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,14 @@ declare_clippy_lint! {
4949

5050
impl<'tcx> QuestionMark {
5151
pub(crate) fn check_manual_let_else(&mut self, cx: &LateContext<'tcx>, stmt: &'tcx Stmt<'tcx>) {
52-
if !self.msrv.meets(msrvs::LET_ELSE) || in_external_macro(cx.sess(), stmt.span) {
53-
return;
54-
}
55-
5652
if let StmtKind::Let(local) = stmt.kind
5753
&& let Some(init) = local.init
5854
&& local.els.is_none()
5955
&& local.ty.is_none()
6056
&& init.span.eq_ctxt(stmt.span)
6157
&& let Some(if_let_or_match) = IfLetOrMatch::parse(cx, init)
58+
&& self.msrv.meets(msrvs::LET_ELSE)
59+
&& !in_external_macro(cx.sess(), stmt.span)
6260
{
6361
match if_let_or_match {
6462
IfLetOrMatch::IfLet(if_let_expr, let_pat, if_then, if_else, ..) => {

clippy_lints/src/manual_main_separator_str.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,13 +47,13 @@ impl_lint_pass!(ManualMainSeparatorStr => [MANUAL_MAIN_SEPARATOR_STR]);
4747

4848
impl LateLintPass<'_> for ManualMainSeparatorStr {
4949
fn check_expr(&mut self, cx: &LateContext<'_>, expr: &Expr<'_>) {
50-
if self.msrv.meets(msrvs::PATH_MAIN_SEPARATOR_STR)
51-
&& let (target, _) = peel_hir_expr_refs(expr)
52-
&& is_trait_method(cx, target, sym::ToString)
53-
&& let ExprKind::MethodCall(path, receiver, &[], _) = target.kind
50+
let (target, _) = peel_hir_expr_refs(expr);
51+
if let ExprKind::MethodCall(path, receiver, &[], _) = target.kind
5452
&& path.ident.name == sym::to_string
5553
&& let ExprKind::Path(QPath::Resolved(None, path)) = receiver.kind
5654
&& let Res::Def(DefKind::Const, receiver_def_id) = path.res
55+
&& is_trait_method(cx, target, sym::ToString)
56+
&& self.msrv.meets(msrvs::PATH_MAIN_SEPARATOR_STR)
5757
&& match_def_path(cx, receiver_def_id, &paths::PATH_MAIN_SEPARATOR)
5858
&& let ty::Ref(_, ty, Mutability::Not) = cx.typeck_results().expr_ty_adjusted(expr).kind()
5959
&& ty.is_str()

0 commit comments

Comments
 (0)