Skip to content

Commit df0c39c

Browse files
borsflip1995
authored andcommitted
Auto merge of #11538 - Jarcho:proc_mac, r=dswij
Fix `is_from_proc_macro` patterns fixes #11533 changelog: none
1 parent b216e7b commit df0c39c

12 files changed

+192
-122
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/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/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)