Skip to content

Commit ea4e12b

Browse files
authored
Call in_external_macro after running other checks in various places (#17294)
This is the fist batch of delayed calls. Not all of the changed locations are ideal, but changing something like the `matches` pass any further would be a lot more involved. changelog: none
2 parents b91807e + c666859 commit ea4e12b

34 files changed

Lines changed: 102 additions & 100 deletions

clippy_lints/src/attrs/allow_attributes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use rustc_lint::{EarlyContext, LintContext as _};
88

99
// Separate each crate's features.
1010
pub fn check<'cx>(cx: &EarlyContext<'cx>, attr: &'cx Attribute) {
11-
if !attr.span.in_external_macro(cx.sess().source_map())
12-
&& let AttrStyle::Outer = attr.style
11+
if let AttrStyle::Outer = attr.style
1312
&& let Some(path_span) = attr.path_span()
13+
&& !attr.span.in_external_macro(cx.sess().source_map())
1414
&& !is_from_proc_macro(cx, attr)
1515
{
1616
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]

clippy_lints/src/attrs/useless_attribute.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
1111
let skip_unused_imports = attrs.iter().any(|attr| attr.has_name(sym::macro_use));
1212

1313
for attr in attrs {
14-
if attr.span.in_external_macro(cx.sess().source_map()) {
15-
return;
16-
}
1714
if let Some(lint_list) = &attr.meta_item_list()
1815
&& attr.name().is_some_and(is_lint_level)
1916
{
@@ -72,9 +69,10 @@ pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
7269
_ => {},
7370
}
7471
}
75-
let line_span = first_line_of_span(cx, attr.span);
7672

77-
if let Some(src) = line_span.get_text(cx)
73+
if !attr.span.in_external_macro(cx.sess().source_map())
74+
&& let line_span = first_line_of_span(cx, attr.span)
75+
&& let Some(src) = line_span.get_text(cx)
7876
&& src.contains("#[")
7977
{
8078
#[expect(clippy::collapsible_span_lint_calls)]

clippy_lints/src/blocks_in_conditions.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use clippy_utils::source::snippet_block_with_applicability;
33
use clippy_utils::{contains_return, higher, is_from_proc_macro, leaks_droppable_temporary};
44
use rustc_errors::Applicability;
55
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
6-
use rustc_lint::{LateContext, LateLintPass, LintContext as _};
6+
use rustc_lint::{LateContext, LateLintPass};
77
use rustc_session::declare_lint_pass;
88

99
declare_clippy_lint! {
@@ -53,7 +53,7 @@ const BRACED_EXPR_MESSAGE: &str = "omit braces around single expression conditio
5353

5454
impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
5555
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
56-
if expr.span.in_external_macro(cx.sess().source_map()) {
56+
if expr.span.from_expansion() {
5757
return;
5858
}
5959

@@ -83,7 +83,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
8383
if let Some(ex) = &block.expr {
8484
// don't dig into the expression here, just suggest that they remove
8585
// the block
86-
if expr.span.from_expansion() || ex.span.from_expansion() {
86+
if ex.span.from_expansion() {
8787
return;
8888
}
8989

@@ -114,7 +114,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
114114
}
115115
} else {
116116
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
117-
if span.from_expansion() || expr.span.from_expansion() || is_from_proc_macro(cx, cond) {
117+
if span.from_expansion() || is_from_proc_macro(cx, cond) {
118118
return;
119119
}
120120
// move block higher

clippy_lints/src/casts/borrow_as_ptr.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub(super) fn check<'tcx>(
2424
&& !is_lint_allowed(cx, BORROW_AS_PTR, expr.hir_id)
2525
// Fix #9884
2626
&& !is_expr_temporary_value(cx, e)
27+
&& !expr.span.in_external_macro(cx.tcx.sess.source_map())
2728
&& !is_from_proc_macro(cx, expr)
2829
{
2930
let mut app = Applicability::MachineApplicable;

clippy_lints/src/casts/cast_ptr_alignment.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ fn lint_cast_ptr_alignment<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'_>, cast_f
3636
// when casting from a ZST, we don't know enough to properly lint
3737
&& !from_layout.is_zst()
3838
&& !is_used_as_unaligned(cx, expr)
39+
&& !expr.span.in_external_macro(cx.tcx.sess.source_map())
3940
{
4041
span_lint(
4142
cx,

clippy_lints/src/casts/cast_slice_different_sizes.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,12 @@ pub(super) fn check<'tcx>(cx: &LateContext<'tcx>, expr: &Expr<'tcx>, msrv: Msrv)
2525
{
2626
let from_size = from_layout.size.bytes();
2727
let to_size = to_layout.size.bytes();
28-
if from_size != to_size && from_size != 0 && to_size != 0 && msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS) {
28+
if from_size != to_size
29+
&& from_size != 0
30+
&& to_size != 0
31+
&& msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS)
32+
&& !expr.span.in_external_macro(cx.tcx.sess.source_map())
33+
{
2934
span_lint_and_then(
3035
cx,
3136
CAST_SLICE_DIFFERENT_SIZES,

clippy_lints/src/casts/cast_slice_from_raw_parts.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub(super) fn check(cx: &LateContext<'_>, expr: &Expr<'_>, cast_expr: &Expr<'_>,
3535
&& let ctxt = expr.span.ctxt()
3636
&& cast_expr.span.ctxt() == ctxt
3737
&& msrv.meets(cx, msrvs::PTR_SLICE_RAW_PARTS)
38+
&& !expr.span.in_external_macro(cx.tcx.sess.source_map())
3839
{
3940
let func = match rpk {
4041
RawPartsKind::Immutable => "from_raw_parts",

clippy_lints/src/casts/mod.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -889,11 +889,10 @@ impl Casts {
889889

890890
impl<'tcx> LateLintPass<'tcx> for Casts {
891891
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
892-
if expr.span.in_external_macro(cx.sess().source_map()) {
893-
return;
894-
}
895-
896892
if let ExprKind::Cast(cast_from_expr, cast_to_hir) = expr.kind {
893+
if expr.span.in_external_macro(cx.sess().source_map()) {
894+
return;
895+
}
897896
if is_hir_ty_cfg_dependant(cx, cast_to_hir) {
898897
return;
899898
}

clippy_lints/src/casts/ptr_cast_constness.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ pub(super) fn check_null_ptr_cast_method(cx: &LateContext<'_>, expr: &Expr<'_>)
9090
&& let mut app = Applicability::MachineApplicable
9191
&& let sugg = snippet_with_applicability(cx, cast_from_expr.span, "_", &mut app)
9292
&& let Some((_, after_lt)) = sugg.split_once("::<")
93+
&& !expr.span.in_external_macro(cx.tcx.sess.source_map())
9394
{
9495
span_lint_and_sugg(
9596
cx,

clippy_lints/src/default_numeric_fallback.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,12 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
8888

8989
/// Check whether a passed literal has potential to cause fallback or not.
9090
fn check_lit(&self, lit: Lit, lit_ty: Ty<'tcx>, emit_hir_id: HirId) {
91-
if !lit.span.in_external_macro(self.cx.sess().source_map())
92-
&& matches!(self.ty_bounds.last(), Some(ExplicitTyBound(false)))
91+
if matches!(self.ty_bounds.last(), Some(ExplicitTyBound(false)))
9392
&& matches!(
9493
lit.node,
9594
LitKind::Int(_, LitIntType::Unsuffixed) | LitKind::Float(_, LitFloatType::Unsuffixed)
9695
)
96+
&& !lit.span.in_external_macro(self.cx.sess().source_map())
9797
{
9898
let (suffix, is_float) = match lit_ty.kind() {
9999
ty::Int(IntTy::I32) => ("i32", false),

0 commit comments

Comments
 (0)