Skip to content

Commit 82e1608

Browse files
committed
Auto merge of #119188 - flip1995:clippy-beta-backport, r=pietroalbini
[beta] Clippy beta backport PR towards stable, as beta was branched a day early and I missed the notification. - rust-lang/rust-clippy#11538 - rust-lang/rust-clippy#11756 - rust-lang/rust-clippy#11760 - rust-lang/rust-clippy#11953 r? `@pietroalbini`
2 parents fbe9b6f + f54362f commit 82e1608

20 files changed

+368
-230
lines changed

src/tools/clippy/clippy_lints/src/as_conversions.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -48,11 +48,10 @@ declare_lint_pass!(AsConversions => [AS_CONVERSIONS]);
4848

4949
impl<'tcx> LateLintPass<'tcx> for AsConversions {
5050
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
51-
if in_external_macro(cx.sess(), expr.span) || is_from_proc_macro(cx, expr) {
52-
return;
53-
}
54-
55-
if let ExprKind::Cast(_, _) = expr.kind {
51+
if let ExprKind::Cast(_, _) = expr.kind
52+
&& !in_external_macro(cx.sess(), expr.span)
53+
&& !is_from_proc_macro(cx, expr)
54+
{
5655
span_lint_and_help(
5756
cx,
5857
AS_CONVERSIONS,

src/tools/clippy/clippy_lints/src/borrow_deref_ref.rs

+51-56
Original file line numberDiff line numberDiff line change
@@ -49,69 +49,64 @@ declare_lint_pass!(BorrowDerefRef => [BORROW_DEREF_REF]);
4949

5050
impl<'tcx> LateLintPass<'tcx> for BorrowDerefRef {
5151
fn check_expr(&mut self, cx: &LateContext<'tcx>, e: &rustc_hir::Expr<'tcx>) {
52-
if_chain! {
53-
if !e.span.from_expansion();
54-
if let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind;
55-
if !addrof_target.span.from_expansion();
56-
if let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind;
57-
if !deref_target.span.from_expansion();
58-
if !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..) );
59-
let ref_ty = cx.typeck_results().expr_ty(deref_target);
60-
if let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind();
61-
if !is_from_proc_macro(cx, e);
62-
then{
63-
64-
if let Some(parent_expr) = get_parent_expr(cx, e){
65-
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..)) &&
66-
!is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id) {
67-
return;
68-
}
52+
if !e.span.from_expansion()
53+
&& let ExprKind::AddrOf(_, Mutability::Not, addrof_target) = e.kind
54+
&& !addrof_target.span.from_expansion()
55+
&& let ExprKind::Unary(UnOp::Deref, deref_target) = addrof_target.kind
56+
&& !deref_target.span.from_expansion()
57+
&& !matches!(deref_target.kind, ExprKind::Unary(UnOp::Deref, ..))
58+
&& let ref_ty = cx.typeck_results().expr_ty(deref_target)
59+
&& let ty::Ref(_, inner_ty, Mutability::Not) = ref_ty.kind()
60+
{
61+
if let Some(parent_expr) = get_parent_expr(cx, e) {
62+
if matches!(parent_expr.kind, ExprKind::Unary(UnOp::Deref, ..))
63+
&& !is_lint_allowed(cx, DEREF_ADDROF, parent_expr.hir_id)
64+
{
65+
return;
66+
}
6967

70-
// modification to `&mut &*x` is different from `&mut x`
71-
if matches!(deref_target.kind, ExprKind::Path(..)
72-
| ExprKind::Field(..)
73-
| ExprKind::Index(..)
74-
| ExprKind::Unary(UnOp::Deref, ..))
75-
&& matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _)) {
76-
return;
77-
}
68+
// modification to `&mut &*x` is different from `&mut x`
69+
if matches!(
70+
deref_target.kind,
71+
ExprKind::Path(..) | ExprKind::Field(..) | ExprKind::Index(..) | ExprKind::Unary(UnOp::Deref, ..)
72+
) && matches!(parent_expr.kind, ExprKind::AddrOf(_, Mutability::Mut, _))
73+
{
74+
return;
7875
}
76+
}
77+
if is_from_proc_macro(cx, e) {
78+
return;
79+
}
7980

80-
span_lint_and_then(
81-
cx,
82-
BORROW_DEREF_REF,
83-
e.span,
84-
"deref on an immutable reference",
85-
|diag| {
86-
diag.span_suggestion(
87-
e.span,
88-
"if you would like to reborrow, try removing `&*`",
89-
snippet_opt(cx, deref_target.span).unwrap(),
90-
Applicability::MachineApplicable
91-
);
81+
span_lint_and_then(
82+
cx,
83+
BORROW_DEREF_REF,
84+
e.span,
85+
"deref on an immutable reference",
86+
|diag| {
87+
diag.span_suggestion(
88+
e.span,
89+
"if you would like to reborrow, try removing `&*`",
90+
snippet_opt(cx, deref_target.span).unwrap(),
91+
Applicability::MachineApplicable,
92+
);
9293

93-
// has deref trait -> give 2 help
94-
// doesn't have deref trait -> give 1 help
95-
if let Some(deref_trait_id) = cx.tcx.lang_items().deref_trait(){
96-
if !implements_trait(cx, *inner_ty, deref_trait_id, &[]) {
97-
return;
98-
}
94+
// has deref trait -> give 2 help
95+
// doesn't have deref trait -> give 1 help
96+
if let Some(deref_trait_id) = cx.tcx.lang_items().deref_trait() {
97+
if !implements_trait(cx, *inner_ty, deref_trait_id, &[]) {
98+
return;
9999
}
100-
101-
diag.span_suggestion(
102-
e.span,
103-
"if you would like to deref, try using `&**`",
104-
format!(
105-
"&**{}",
106-
&snippet_opt(cx, deref_target.span).unwrap(),
107-
),
108-
Applicability::MaybeIncorrect
109-
);
110-
111100
}
112-
);
113101

114-
}
102+
diag.span_suggestion(
103+
e.span,
104+
"if you would like to deref, try using `&**`",
105+
format!("&**{}", &snippet_opt(cx, deref_target.span).unwrap()),
106+
Applicability::MaybeIncorrect,
107+
);
108+
},
109+
);
115110
}
116111
}
117112
}

src/tools/clippy/clippy_lints/src/loops/unused_enumerate_index.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty;
99

1010
/// Checks for the `UNUSED_ENUMERATE_INDEX` lint.
1111
pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx Expr<'_>, body: &'tcx Expr<'_>) {
12-
let PatKind::Tuple(tuple, _) = pat.kind else {
12+
let PatKind::Tuple([index, elem], _) = pat.kind else {
1313
return;
1414
};
1515

@@ -19,7 +19,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx
1919

2020
let ty = cx.typeck_results().expr_ty(arg);
2121

22-
if !pat_is_wild(cx, &tuple[0].kind, body) {
22+
if !pat_is_wild(cx, &index.kind, body) {
2323
return;
2424
}
2525

@@ -53,7 +53,7 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, pat: &'tcx Pat<'_>, arg: &'tcx
5353
diag,
5454
"remove the `.enumerate()` call",
5555
vec![
56-
(pat.span, snippet(cx, tuple[1].span, "..").into_owned()),
56+
(pat.span, snippet(cx, elem.span, "..").into_owned()),
5757
(arg.span, base_iter.to_string()),
5858
],
5959
);

src/tools/clippy/clippy_lints/src/manual_float_methods.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -105,14 +105,16 @@ impl<'tcx> LateLintPass<'tcx> for ManualFloatMethods {
105105
// case somebody does that for some reason
106106
&& (is_infinity(const_1) && is_neg_infinity(const_2)
107107
|| is_neg_infinity(const_1) && is_infinity(const_2))
108-
&& !is_from_proc_macro(cx, expr)
109108
&& let Some(local_snippet) = snippet_opt(cx, first.span)
110109
{
111110
let variant = match (kind.node, lhs_kind.node, rhs_kind.node) {
112111
(BinOpKind::Or, BinOpKind::Eq, BinOpKind::Eq) => Variant::ManualIsInfinite,
113112
(BinOpKind::And, BinOpKind::Ne, BinOpKind::Ne) => Variant::ManualIsFinite,
114113
_ => return,
115114
};
115+
if is_from_proc_macro(cx, expr) {
116+
return;
117+
}
116118

117119
span_lint_and_then(cx, variant.lint(), expr.span, variant.msg(), |diag| {
118120
match variant {

src/tools/clippy/clippy_lints/src/methods/unnecessary_lazy_eval.rs

+1-5
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@ pub(super) fn check<'tcx>(
1919
arg: &'tcx hir::Expr<'_>,
2020
simplify_using: &str,
2121
) {
22-
if is_from_proc_macro(cx, expr) {
23-
return;
24-
}
25-
2622
let is_option = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Option);
2723
let is_result = is_type_diagnostic_item(cx, cx.typeck_results().expr_ty(recv), sym::Result);
2824
let is_bool = cx.typeck_results().expr_ty(recv).is_bool();
@@ -32,7 +28,7 @@ pub(super) fn check<'tcx>(
3228
let body = cx.tcx.hir().body(body);
3329
let body_expr = &body.value;
3430

35-
if usage::BindingUsageFinder::are_params_used(cx, body) {
31+
if usage::BindingUsageFinder::are_params_used(cx, body) || is_from_proc_macro(cx, expr) {
3632
return;
3733
}
3834

src/tools/clippy/clippy_lints/src/methods/unnecessary_to_owned.rs

+15-16
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use rustc_lint::LateContext;
1515
use rustc_middle::mir::Mutability;
1616
use rustc_middle::ty::adjustment::{Adjust, Adjustment, OverloadedDeref};
1717
use rustc_middle::ty::{
18-
self, ClauseKind, EarlyBinder, GenericArg, GenericArgKind, GenericArgsRef, ParamTy, ProjectionPredicate,
19-
TraitPredicate, Ty,
18+
self, ClauseKind, GenericArg, GenericArgKind, GenericArgsRef, ParamTy, ProjectionPredicate, TraitPredicate, Ty,
2019
};
2120
use rustc_span::{sym, Symbol};
2221
use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt as _;
@@ -375,6 +374,7 @@ fn get_input_traits_and_projections<'tcx>(
375374
(trait_predicates, projection_predicates)
376375
}
377376

377+
#[expect(clippy::too_many_lines)]
378378
fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<'a>) -> bool {
379379
for (_, node) in cx.tcx.hir().parent_iter(expr.hir_id) {
380380
match node {
@@ -403,22 +403,21 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
403403
if let Some((callee_def_id, call_generic_args, recv, call_args)) =
404404
get_callee_generic_args_and_args(cx, parent_expr)
405405
{
406-
// FIXME: the `instantiate_identity()` below seems incorrect, since we eventually
407-
// call `tcx.try_instantiate_and_normalize_erasing_regions` further down
408-
// (i.e., we are explicitly not in the identity context).
409-
let fn_sig = cx.tcx.fn_sig(callee_def_id).instantiate_identity().skip_binder();
406+
let bound_fn_sig = cx.tcx.fn_sig(callee_def_id);
407+
let fn_sig = bound_fn_sig.skip_binder();
410408
if let Some(arg_index) = recv.into_iter().chain(call_args).position(|arg| arg.hir_id == expr.hir_id)
411-
&& let Some(param_ty) = fn_sig.inputs().get(arg_index)
412-
&& let ty::Param(ParamTy { index: param_index , ..}) = param_ty.kind()
409+
&& let param_ty = fn_sig.input(arg_index).skip_binder()
410+
&& let ty::Param(ParamTy { index: param_index , ..}) = *param_ty.kind()
413411
// https://github.com/rust-lang/rust-clippy/issues/9504 and https://github.com/rust-lang/rust-clippy/issues/10021
414-
&& (*param_index as usize) < call_generic_args.len()
412+
&& (param_index as usize) < call_generic_args.len()
415413
{
416414
if fn_sig
415+
.skip_binder()
417416
.inputs()
418417
.iter()
419418
.enumerate()
420419
.filter(|(i, _)| *i != arg_index)
421-
.any(|(_, ty)| ty.contains(*param_ty))
420+
.any(|(_, ty)| ty.contains(param_ty))
422421
{
423422
return false;
424423
}
@@ -430,7 +429,7 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
430429
.iter()
431430
.filter(|predicate| {
432431
if let ClauseKind::Trait(trait_predicate) = predicate.kind().skip_binder()
433-
&& trait_predicate.trait_ref.self_ty() == *param_ty
432+
&& trait_predicate.trait_ref.self_ty() == param_ty
434433
{
435434
true
436435
} else {
@@ -441,15 +440,15 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
441440
let new_subst = cx
442441
.tcx
443442
.mk_args_from_iter(call_generic_args.iter().enumerate().map(|(i, t)| {
444-
if i == (*param_index as usize) {
443+
if i == param_index as usize {
445444
GenericArg::from(ty)
446445
} else {
447446
t
448447
}
449448
}));
450449

451450
if trait_predicates.any(|predicate| {
452-
let predicate = EarlyBinder::bind(predicate).instantiate(cx.tcx, new_subst);
451+
let predicate = bound_fn_sig.rebind(predicate).instantiate(cx.tcx, new_subst);
453452
let obligation = Obligation::new(cx.tcx, ObligationCause::dummy(), cx.param_env, predicate);
454453
!cx.tcx
455454
.infer_ctxt()
@@ -459,12 +458,12 @@ fn can_change_type<'a>(cx: &LateContext<'a>, mut expr: &'a Expr<'a>, mut ty: Ty<
459458
return false;
460459
}
461460

462-
let output_ty = fn_sig.output();
463-
if output_ty.contains(*param_ty) {
461+
let output_ty = cx.tcx.erase_late_bound_regions(fn_sig.output());
462+
if output_ty.contains(param_ty) {
464463
if let Ok(new_ty) = cx.tcx.try_instantiate_and_normalize_erasing_regions(
465464
new_subst,
466465
cx.param_env,
467-
EarlyBinder::bind(output_ty),
466+
bound_fn_sig.rebind(output_ty),
468467
) {
469468
expr = parent_expr;
470469
ty = new_ty;

src/tools/clippy/clippy_lints/src/needless_if.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,6 @@ impl LateLintPass<'_> for NeedlessIf {
4444
&& block.stmts.is_empty()
4545
&& block.expr.is_none()
4646
&& !in_external_macro(cx.sess(), expr.span)
47-
&& !is_from_proc_macro(cx, expr)
4847
&& let Some(then_snippet) = snippet_opt(cx, then.span)
4948
// Ignore
5049
// - empty macro expansions
@@ -53,6 +52,7 @@ impl LateLintPass<'_> for NeedlessIf {
5352
// - #[cfg]'d out code
5453
&& then_snippet.chars().all(|ch| matches!(ch, '{' | '}') || ch.is_ascii_whitespace())
5554
&& let Some(cond_snippet) = snippet_opt(cx, cond.span)
55+
&& !is_from_proc_macro(cx, expr)
5656
{
5757
span_lint_and_sugg(
5858
cx,

src/tools/clippy/clippy_lints/src/operators/arithmetic_side_effects.rs

+14-11
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,11 @@ impl ArithmeticSideEffects {
132132
}
133133

134134
// Common entry-point to avoid code duplication.
135-
fn issue_lint(&mut self, cx: &LateContext<'_>, expr: &hir::Expr<'_>) {
135+
fn issue_lint<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'_>) {
136+
if is_from_proc_macro(cx, expr) {
137+
return;
138+
}
139+
136140
let msg = "arithmetic operation that can potentially result in unexpected side-effects";
137141
span_lint(cx, ARITHMETIC_SIDE_EFFECTS, expr.span, msg);
138142
self.expr_span = Some(expr.span);
@@ -160,10 +164,10 @@ impl ArithmeticSideEffects {
160164
fn manage_bin_ops<'tcx>(
161165
&mut self,
162166
cx: &LateContext<'tcx>,
163-
expr: &hir::Expr<'tcx>,
167+
expr: &'tcx hir::Expr<'_>,
164168
op: &Spanned<hir::BinOpKind>,
165-
lhs: &hir::Expr<'tcx>,
166-
rhs: &hir::Expr<'tcx>,
169+
lhs: &'tcx hir::Expr<'_>,
170+
rhs: &'tcx hir::Expr<'_>,
167171
) {
168172
if constant_simple(cx, cx.typeck_results(), expr).is_some() {
169173
return;
@@ -236,10 +240,10 @@ impl ArithmeticSideEffects {
236240
/// provided input.
237241
fn manage_method_call<'tcx>(
238242
&mut self,
239-
args: &[hir::Expr<'tcx>],
243+
args: &'tcx [hir::Expr<'_>],
240244
cx: &LateContext<'tcx>,
241-
ps: &hir::PathSegment<'tcx>,
242-
receiver: &hir::Expr<'tcx>,
245+
ps: &'tcx hir::PathSegment<'_>,
246+
receiver: &'tcx hir::Expr<'_>,
243247
) {
244248
let Some(arg) = args.first() else {
245249
return;
@@ -264,8 +268,8 @@ impl ArithmeticSideEffects {
264268
fn manage_unary_ops<'tcx>(
265269
&mut self,
266270
cx: &LateContext<'tcx>,
267-
expr: &hir::Expr<'tcx>,
268-
un_expr: &hir::Expr<'tcx>,
271+
expr: &'tcx hir::Expr<'_>,
272+
un_expr: &'tcx hir::Expr<'_>,
269273
un_op: hir::UnOp,
270274
) {
271275
let hir::UnOp::Neg = un_op else {
@@ -287,14 +291,13 @@ impl ArithmeticSideEffects {
287291

288292
fn should_skip_expr<'tcx>(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) -> bool {
289293
is_lint_allowed(cx, ARITHMETIC_SIDE_EFFECTS, expr.hir_id)
290-
|| is_from_proc_macro(cx, expr)
291294
|| self.expr_span.is_some()
292295
|| self.const_span.map_or(false, |sp| sp.contains(expr.span))
293296
}
294297
}
295298

296299
impl<'tcx> LateLintPass<'tcx> for ArithmeticSideEffects {
297-
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &hir::Expr<'tcx>) {
300+
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx hir::Expr<'tcx>) {
298301
if self.should_skip_expr(cx, expr) {
299302
return;
300303
}

src/tools/clippy/clippy_lints/src/single_call_fn.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,8 @@ impl<'tcx> LateLintPass<'tcx> for SingleCallFn {
7272
) {
7373
if self.avoid_breaking_exported_api && cx.effective_visibilities.is_exported(def_id)
7474
|| in_external_macro(cx.sess(), span)
75-
|| is_from_proc_macro(cx, &(&kind, body, cx.tcx.local_def_id_to_hir_id(def_id), span))
7675
|| is_in_test_function(cx.tcx, body.value.hir_id)
76+
|| is_from_proc_macro(cx, &(&kind, body, cx.tcx.local_def_id_to_hir_id(def_id), span))
7777
{
7878
return;
7979
}

0 commit comments

Comments
 (0)