Skip to content

Commit b3c94c0

Browse files
committed
Auto merge of #8976 - xFrednet:rust-97660-catch-emissions-with-expect, r=Jarcho
Fix some `#[expect]` lint interaction Fixing the first few lints that aren't caught by `#[expect]`. The root cause of these examples was, that the lint was emitted at the wrong location. --- changelog: none r? `@Jarcho` cc: rust-lang/rust#97660
2 parents 65f518e + 9d201d6 commit b3c94c0

16 files changed

+186
-95
lines changed

clippy_lints/src/async_yields_async.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_then;
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::source::snippet;
33
use clippy_utils::ty::implements_trait;
44
use rustc_errors::Applicability;
@@ -64,9 +64,10 @@ impl<'tcx> LateLintPass<'tcx> for AsyncYieldsAsync {
6464
_ => None,
6565
};
6666
if let Some(return_expr_span) = return_expr_span {
67-
span_lint_and_then(
67+
span_lint_hir_and_then(
6868
cx,
6969
ASYNC_YIELDS_ASYNC,
70+
body.value.hir_id,
7071
return_expr_span,
7172
"an async construct yields a type which is itself awaitable",
7273
|db| {

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::span_lint_and_sugg;
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use clippy_utils::numeric_literal;
33
use clippy_utils::source::snippet_opt;
44
use if_chain::if_chain;
@@ -76,7 +76,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
7676
}
7777

7878
/// Check whether a passed literal has potential to cause fallback or not.
79-
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>) {
79+
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>, emit_hir_id: HirId) {
8080
if_chain! {
8181
if !in_external_macro(self.cx.sess(), lit.span);
8282
if let Some(ty_bound) = self.ty_bounds.last();
@@ -101,14 +101,15 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
101101
}
102102
};
103103
let sugg = numeric_literal::format(&src, Some(suffix), is_float);
104-
span_lint_and_sugg(
104+
span_lint_hir_and_then(
105105
self.cx,
106106
DEFAULT_NUMERIC_FALLBACK,
107+
emit_hir_id,
107108
lit.span,
108109
"default numeric fallback might occur",
109-
"consider adding suffix",
110-
sugg,
111-
Applicability::MaybeIncorrect,
110+
|diag| {
111+
diag.span_suggestion(lit.span, "consider adding suffix", sugg, Applicability::MaybeIncorrect);
112+
}
112113
);
113114
}
114115
}
@@ -179,7 +180,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {
179180

180181
ExprKind::Lit(lit) => {
181182
let ty = self.cx.typeck_results().expr_ty(expr);
182-
self.check_lit(lit, ty);
183+
self.check_lit(lit, ty, expr.hir_id);
183184
return;
184185
},
185186

clippy_lints/src/dereference.rs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
1+
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
22
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
33
use clippy_utils::sugg::has_enclosing_paren;
44
use clippy_utils::ty::peel_mid_ty_refs;
@@ -135,6 +135,7 @@ pub struct Dereferencing {
135135
struct StateData {
136136
/// Span of the top level expression
137137
span: Span,
138+
hir_id: HirId,
138139
}
139140

140141
enum State {
@@ -169,6 +170,8 @@ struct RefPat {
169170
app: Applicability,
170171
/// All the replacements which need to be made.
171172
replacements: Vec<(Span, String)>,
173+
/// The [`HirId`] that the lint should be emitted at.
174+
hir_id: HirId,
172175
}
173176

174177
impl<'tcx> LateLintPass<'tcx> for Dereferencing {
@@ -222,7 +225,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
222225
is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
223226
target_mut,
224227
},
225-
StateData { span: expr.span },
228+
StateData {
229+
span: expr.span,
230+
hir_id: expr.hir_id,
231+
},
226232
));
227233
},
228234
RefOp::AddrOf => {
@@ -294,7 +300,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
294300
required_precedence,
295301
msg,
296302
},
297-
StateData { span: expr.span },
303+
StateData {
304+
span: expr.span,
305+
hir_id: expr.hir_id,
306+
},
298307
));
299308
}
300309
},
@@ -387,6 +396,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
387396
spans: vec![pat.span],
388397
app,
389398
replacements: vec![(pat.span, snip.into())],
399+
hir_id: pat.hir_id
390400
}),
391401
);
392402
}
@@ -399,13 +409,15 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
399409
for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
400410
let replacements = pat.replacements;
401411
let app = pat.app;
402-
span_lint_and_then(
412+
let lint = if pat.always_deref {
413+
NEEDLESS_BORROW
414+
} else {
415+
REF_BINDING_TO_REFERENCE
416+
};
417+
span_lint_hir_and_then(
403418
cx,
404-
if pat.always_deref {
405-
NEEDLESS_BORROW
406-
} else {
407-
REF_BINDING_TO_REFERENCE
408-
},
419+
lint,
420+
pat.hir_id,
409421
pat.spans,
410422
"this pattern creates a reference to a reference",
411423
|diag| {
@@ -642,19 +654,14 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, state: State, data: S
642654
} => {
643655
let mut app = Applicability::MachineApplicable;
644656
let snip = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app).0;
645-
span_lint_and_sugg(
646-
cx,
647-
NEEDLESS_BORROW,
648-
data.span,
649-
msg,
650-
"change this to",
651-
if required_precedence > expr.precedence().order() && !has_enclosing_paren(&snip) {
657+
span_lint_hir_and_then(cx, NEEDLESS_BORROW, data.hir_id, data.span, msg, |diag| {
658+
let sugg = if required_precedence > expr.precedence().order() && !has_enclosing_paren(&snip) {
652659
format!("({})", snip)
653660
} else {
654661
snip.into()
655-
},
656-
app,
657-
);
662+
};
663+
diag.span_suggestion(data.span, "change this to", sugg, app);
664+
});
658665
},
659666
}
660667
}

clippy_lints/src/same_name_method.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use clippy_utils::diagnostics::span_lint_and_then;
1+
use clippy_utils::diagnostics::span_lint_hir_and_then;
22
use rustc_data_structures::fx::FxHashMap;
33
use rustc_hir::def::{DefKind, Res};
4-
use rustc_hir::{Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
4+
use rustc_hir::{HirId, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
55
use rustc_lint::{LateContext, LateLintPass};
66
use rustc_middle::ty::AssocKind;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
@@ -42,11 +42,12 @@ declare_clippy_lint! {
4242
declare_lint_pass!(SameNameMethod => [SAME_NAME_METHOD]);
4343

4444
struct ExistingName {
45-
impl_methods: BTreeMap<Symbol, Span>,
45+
impl_methods: BTreeMap<Symbol, (Span, HirId)>,
4646
trait_methods: BTreeMap<Symbol, Vec<Span>>,
4747
}
4848

4949
impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
50+
#[expect(clippy::too_many_lines)]
5051
fn check_crate_post(&mut self, cx: &LateContext<'tcx>) {
5152
let mut map = FxHashMap::<Res, ExistingName>::default();
5253

@@ -97,10 +98,11 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
9798
};
9899

99100
let mut check_trait_method = |method_name: Symbol, trait_method_span: Span| {
100-
if let Some(impl_span) = existing_name.impl_methods.get(&method_name) {
101-
span_lint_and_then(
101+
if let Some((impl_span, hir_id)) = existing_name.impl_methods.get(&method_name) {
102+
span_lint_hir_and_then(
102103
cx,
103104
SAME_NAME_METHOD,
105+
*hir_id,
104106
*impl_span,
105107
"method's name is the same as an existing method in a trait",
106108
|diag| {
@@ -136,10 +138,12 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
136138
}) {
137139
let method_name = impl_item_ref.ident.name;
138140
let impl_span = impl_item_ref.span;
141+
let hir_id = impl_item_ref.id.hir_id();
139142
if let Some(trait_spans) = existing_name.trait_methods.get(&method_name) {
140-
span_lint_and_then(
143+
span_lint_hir_and_then(
141144
cx,
142145
SAME_NAME_METHOD,
146+
hir_id,
143147
impl_span,
144148
"method's name is the same as an existing method in a trait",
145149
|diag| {
@@ -152,7 +156,7 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
152156
},
153157
);
154158
}
155-
existing_name.impl_methods.insert(method_name, impl_span);
159+
existing_name.impl_methods.insert(method_name, (impl_span, hir_id));
156160
}
157161
},
158162
}

tests/ui/async_yields_async.fixed

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
2+
#![feature(lint_reasons)]
33
#![feature(async_closure)]
44
#![warn(clippy::async_yields_async)]
55

@@ -65,3 +65,14 @@ fn main() {
6565
let _n = async || custom_future_type_ctor();
6666
let _o = async || f();
6767
}
68+
69+
#[rustfmt::skip]
70+
#[allow(dead_code)]
71+
fn check_expect_suppression() {
72+
#[expect(clippy::async_yields_async)]
73+
let _j = async || {
74+
async {
75+
3
76+
}
77+
};
78+
}

tests/ui/async_yields_async.rs

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// run-rustfix
2-
2+
#![feature(lint_reasons)]
33
#![feature(async_closure)]
44
#![warn(clippy::async_yields_async)]
55

@@ -65,3 +65,14 @@ fn main() {
6565
let _n = async || custom_future_type_ctor();
6666
let _o = async || f();
6767
}
68+
69+
#[rustfmt::skip]
70+
#[allow(dead_code)]
71+
fn check_expect_suppression() {
72+
#[expect(clippy::async_yields_async)]
73+
let _j = async || {
74+
async {
75+
3
76+
}
77+
};
78+
}

tests/ui/default_numeric_fallback_i32.fixed

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-rustfix
22
// aux-build:macro_rules.rs
33

4+
#![feature(lint_reasons)]
45
#![warn(clippy::default_numeric_fallback)]
56
#![allow(
67
unused,
@@ -173,4 +174,9 @@ mod in_macro {
173174
}
174175
}
175176

177+
fn check_expect_suppression() {
178+
#[expect(clippy::default_numeric_fallback)]
179+
let x = 21;
180+
}
181+
176182
fn main() {}

tests/ui/default_numeric_fallback_i32.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// run-rustfix
22
// aux-build:macro_rules.rs
33

4+
#![feature(lint_reasons)]
45
#![warn(clippy::default_numeric_fallback)]
56
#![allow(
67
unused,
@@ -173,4 +174,9 @@ mod in_macro {
173174
}
174175
}
175176

177+
fn check_expect_suppression() {
178+
#[expect(clippy::default_numeric_fallback)]
179+
let x = 21;
180+
}
181+
176182
fn main() {}

0 commit comments

Comments
 (0)