Skip to content

Commit

Permalink
Auto merge of #8976 - xFrednet:rust-97660-catch-emissions-with-expect…
Browse files Browse the repository at this point in the history
…, 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
  • Loading branch information
bors committed Jun 9, 2022
2 parents 65f518e + 9d201d6 commit b3c94c0
Show file tree
Hide file tree
Showing 16 changed files with 186 additions and 95 deletions.
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 @@ -64,9 +64,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 @@ -135,6 +135,7 @@ pub struct Dereferencing {
struct StateData {
/// Span of the top level expression
span: Span,
hir_id: HirId,
}

enum State {
Expand Down Expand Up @@ -169,6 +170,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 @@ -222,7 +225,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 @@ -294,7 +300,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 @@ -387,6 +396,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 @@ -399,13 +409,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 @@ -642,19 +654,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

0 comments on commit b3c94c0

Please sign in to comment.