Skip to content

Fix some #[expect] lint interaction #8976

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions clippy_lints/src/async_yields_async.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::source::snippet;
use clippy_utils::ty::implements_trait;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -63,9 +63,10 @@ impl<'tcx> LateLintPass<'tcx> for AsyncYieldsAsync {
_ => None,
};
if let Some(return_expr_span) = return_expr_span {
span_lint_and_then(
span_lint_hir_and_then(
cx,
ASYNC_YIELDS_ASYNC,
body.value.hir_id,
return_expr_span,
"an async construct yields a type which is itself awaitable",
|db| {
Expand Down
15 changes: 8 additions & 7 deletions clippy_lints/src/default_numeric_fallback.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::diagnostics::span_lint_hir_and_then;
use clippy_utils::numeric_literal;
use clippy_utils::source::snippet_opt;
use if_chain::if_chain;
Expand Down Expand Up @@ -76,7 +76,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
}

/// Check whether a passed literal has potential to cause fallback or not.
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>) {
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>, emit_hir_id: HirId) {
if_chain! {
if !in_external_macro(self.cx.sess(), lit.span);
if let Some(ty_bound) = self.ty_bounds.last();
Expand All @@ -101,14 +101,15 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
}
};
let sugg = numeric_literal::format(&src, Some(suffix), is_float);
span_lint_and_sugg(
span_lint_hir_and_then(
self.cx,
DEFAULT_NUMERIC_FALLBACK,
emit_hir_id,
lit.span,
"default numeric fallback might occur",
"consider adding suffix",
sugg,
Applicability::MaybeIncorrect,
|diag| {
diag.span_suggestion(lit.span, "consider adding suffix", sugg, Applicability::MaybeIncorrect);
}
);
}
}
Expand Down Expand Up @@ -179,7 +180,7 @@ impl<'a, 'tcx> Visitor<'tcx> for NumericFallbackVisitor<'a, 'tcx> {

ExprKind::Lit(lit) => {
let ty = self.cx.typeck_results().expr_ty(expr);
self.check_lit(lit, ty);
self.check_lit(lit, ty, expr.hir_id);
return;
},

Expand Down
45 changes: 26 additions & 19 deletions clippy_lints/src/dereference.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_hir_and_then};
use clippy_utils::source::{snippet_with_applicability, snippet_with_context};
use clippy_utils::sugg::has_enclosing_paren;
use clippy_utils::ty::peel_mid_ty_refs;
Expand Down Expand Up @@ -131,6 +131,7 @@ pub struct Dereferencing {
struct StateData {
/// Span of the top level expression
span: Span,
hir_id: HirId,
}

enum State {
Expand Down Expand Up @@ -165,6 +166,8 @@ struct RefPat {
app: Applicability,
/// All the replacements which need to be made.
replacements: Vec<(Span, String)>,
/// The [`HirId`] that the lint should be emitted at.
hir_id: HirId,
}

impl<'tcx> LateLintPass<'tcx> for Dereferencing {
Expand Down Expand Up @@ -218,7 +221,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
is_final_ufcs: matches!(expr.kind, ExprKind::Call(..)),
target_mut,
},
StateData { span: expr.span },
StateData {
span: expr.span,
hir_id: expr.hir_id,
},
));
},
RefOp::AddrOf => {
Expand Down Expand Up @@ -290,7 +296,10 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
required_precedence,
msg,
},
StateData { span: expr.span },
StateData {
span: expr.span,
hir_id: expr.hir_id,
},
));
}
},
Expand Down Expand Up @@ -383,6 +392,7 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
spans: vec![pat.span],
app,
replacements: vec![(pat.span, snip.into())],
hir_id: pat.hir_id
}),
);
}
Expand All @@ -395,13 +405,15 @@ impl<'tcx> LateLintPass<'tcx> for Dereferencing {
for pat in self.ref_locals.drain(..).filter_map(|(_, x)| x) {
let replacements = pat.replacements;
let app = pat.app;
span_lint_and_then(
let lint = if pat.always_deref {
NEEDLESS_BORROW
} else {
REF_BINDING_TO_REFERENCE
};
span_lint_hir_and_then(
cx,
if pat.always_deref {
NEEDLESS_BORROW
} else {
REF_BINDING_TO_REFERENCE
},
lint,
pat.hir_id,
pat.spans,
"this pattern creates a reference to a reference",
|diag| {
Expand Down Expand Up @@ -638,19 +650,14 @@ fn report<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, state: State, data: S
} => {
let mut app = Applicability::MachineApplicable;
let snip = snippet_with_context(cx, expr.span, data.span.ctxt(), "..", &mut app).0;
span_lint_and_sugg(
cx,
NEEDLESS_BORROW,
data.span,
msg,
"change this to",
if required_precedence > expr.precedence().order() && !has_enclosing_paren(&snip) {
span_lint_hir_and_then(cx, NEEDLESS_BORROW, data.hir_id, data.span, msg, |diag| {
let sugg = if required_precedence > expr.precedence().order() && !has_enclosing_paren(&snip) {
format!("({})", snip)
} else {
snip.into()
},
app,
);
};
diag.span_suggestion(data.span, "change this to", sugg, app);
});
},
}
}
Expand Down
18 changes: 11 additions & 7 deletions clippy_lints/src/same_name_method.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use clippy_utils::diagnostics::span_lint_and_then;
use clippy_utils::diagnostics::span_lint_hir_and_then;
use rustc_data_structures::fx::FxHashMap;
use rustc_hir::def::{DefKind, Res};
use rustc_hir::{Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
use rustc_hir::{HirId, Impl, ItemKind, Node, Path, QPath, TraitRef, TyKind};
use rustc_lint::{LateContext, LateLintPass};
use rustc_middle::ty::AssocKind;
use rustc_session::{declare_lint_pass, declare_tool_lint};
Expand Down Expand Up @@ -42,11 +42,12 @@ declare_clippy_lint! {
declare_lint_pass!(SameNameMethod => [SAME_NAME_METHOD]);

struct ExistingName {
impl_methods: BTreeMap<Symbol, Span>,
impl_methods: BTreeMap<Symbol, (Span, HirId)>,
trait_methods: BTreeMap<Symbol, Vec<Span>>,
}

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

Expand Down Expand Up @@ -97,10 +98,11 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
};

let mut check_trait_method = |method_name: Symbol, trait_method_span: Span| {
if let Some(impl_span) = existing_name.impl_methods.get(&method_name) {
span_lint_and_then(
if let Some((impl_span, hir_id)) = existing_name.impl_methods.get(&method_name) {
span_lint_hir_and_then(
cx,
SAME_NAME_METHOD,
*hir_id,
*impl_span,
"method's name is the same as an existing method in a trait",
|diag| {
Expand Down Expand Up @@ -136,10 +138,12 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
}) {
let method_name = impl_item_ref.ident.name;
let impl_span = impl_item_ref.span;
let hir_id = impl_item_ref.id.hir_id();
if let Some(trait_spans) = existing_name.trait_methods.get(&method_name) {
span_lint_and_then(
span_lint_hir_and_then(
cx,
SAME_NAME_METHOD,
hir_id,
impl_span,
"method's name is the same as an existing method in a trait",
|diag| {
Expand All @@ -152,7 +156,7 @@ impl<'tcx> LateLintPass<'tcx> for SameNameMethod {
},
);
}
existing_name.impl_methods.insert(method_name, impl_span);
existing_name.impl_methods.insert(method_name, (impl_span, hir_id));
}
},
}
Expand Down
13 changes: 12 additions & 1 deletion tests/ui/async_yields_async.fixed
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix

#![feature(lint_reasons)]
#![feature(async_closure)]
#![warn(clippy::async_yields_async)]

Expand Down Expand Up @@ -65,3 +65,14 @@ fn main() {
let _n = async || custom_future_type_ctor();
let _o = async || f();
}

#[rustfmt::skip]
#[allow(dead_code)]
fn check_expect_suppression() {
#[expect(clippy::async_yields_async)]
let _j = async || {
async {
3
}
};
}
13 changes: 12 additions & 1 deletion tests/ui/async_yields_async.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// run-rustfix

#![feature(lint_reasons)]
#![feature(async_closure)]
#![warn(clippy::async_yields_async)]

Expand Down Expand Up @@ -65,3 +65,14 @@ fn main() {
let _n = async || custom_future_type_ctor();
let _o = async || f();
}

#[rustfmt::skip]
#[allow(dead_code)]
fn check_expect_suppression() {
#[expect(clippy::async_yields_async)]
let _j = async || {
async {
3
}
};
}
6 changes: 6 additions & 0 deletions tests/ui/default_numeric_fallback_i32.fixed
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// run-rustfix
// aux-build:macro_rules.rs

#![feature(lint_reasons)]
#![warn(clippy::default_numeric_fallback)]
#![allow(
unused,
Expand Down Expand Up @@ -173,4 +174,9 @@ mod in_macro {
}
}

fn check_expect_suppression() {
#[expect(clippy::default_numeric_fallback)]
let x = 21;
}

fn main() {}
6 changes: 6 additions & 0 deletions tests/ui/default_numeric_fallback_i32.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// run-rustfix
// aux-build:macro_rules.rs

#![feature(lint_reasons)]
#![warn(clippy::default_numeric_fallback)]
#![allow(
unused,
Expand Down Expand Up @@ -173,4 +174,9 @@ mod in_macro {
}
}

fn check_expect_suppression() {
#[expect(clippy::default_numeric_fallback)]
let x = 21;
}

fn main() {}
Loading