Skip to content

Commit 3e3715c

Browse files
authored
r? @ghost changelog: none
2 parents 660d861 + 9f31768 commit 3e3715c

File tree

138 files changed

+597
-464
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

138 files changed

+597
-464
lines changed

book/src/development/adding_lints.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -788,7 +788,7 @@ don't hesitate to ask on [Zulip] or in the issue/PR.
788788
[`snippet`]: https://doc.rust-lang.org/nightly/nightly-rustc/clippy_utils/source/fn.snippet.html
789789
[let-chains]: https://github.com/rust-lang/rust/pull/94927
790790
[from_expansion]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html#method.from_expansion
791-
[in_external_macro]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/lint/fn.in_external_macro.html
791+
[in_external_macro]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html#method.in_external_macro
792792
[span]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.Span.html
793793
[applicability]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_errors/enum.Applicability.html
794794
[rustc-dev-guide]: https://rustc-dev-guide.rust-lang.org/

book/src/development/common_tools_writing_lints.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,7 @@ functions to deal with macros:
218218
> context. And so just using `span.from_expansion()` is often good enough.
219219
220220

221-
- `in_external_macro(span)`: detect if the given span is from a macro defined in
221+
- `span.in_external_macro(sm)`: detect if the given span is from a macro defined in
222222
a foreign crate. If you want the lint to work with macro-generated code, this
223223
is the next line of defense to avoid macros not defined in the current crate.
224224
It doesn't make sense to lint code that the coder can't change.
@@ -227,15 +227,13 @@ functions to deal with macros:
227227
crates
228228

229229
```rust
230-
use rustc_middle::lint::in_external_macro;
231-
232230
use a_crate_with_macros::foo;
233231

234232
// `foo` is defined in `a_crate_with_macros`
235233
foo!("bar");
236234

237235
// if we lint the `match` of `foo` call and test its span
238-
assert_eq!(in_external_macro(cx.sess(), match_span), true);
236+
assert_eq!(match_span.in_external_macro(cx.sess().source_map()), true);
239237
```
240238

241239
- `span.ctxt()`: the span's context represents whether it is from expansion, and

book/src/development/macro_expansions.md

+3-3
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ assert_ne!(x_is_some_span.ctxt(), x_unwrap_span.ctxt());
120120

121121
### The `in_external_macro` function
122122

123-
`rustc_middle::lint` provides a function ([`in_external_macro`]) that can
123+
`Span` provides a method ([`in_external_macro`]) that can
124124
detect if the given span is from a macro defined in a foreign crate.
125125

126126
Therefore, if we really want a new lint to work with macro-generated code,
@@ -144,7 +144,7 @@ Also assume that we get the corresponding variable `foo_span` for the
144144
results in `true` (note that `cx` can be `EarlyContext` or `LateContext`):
145145

146146
```rust
147-
if in_external_macro(cx.sess(), foo_span) {
147+
if foo_span.in_external_macro(cx.sess().source_map()) {
148148
// We should ignore macro from a foreign crate.
149149
return;
150150
}
@@ -153,6 +153,6 @@ if in_external_macro(cx.sess(), foo_span) {
153153
[`ctxt`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/struct.Span.html#method.ctxt
154154
[expansion]: https://rustc-dev-guide.rust-lang.org/macro-expansion.html#expansion-and-ast-integration
155155
[`from_expansion`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/struct.Span.html#method.from_expansion
156-
[`in_external_macro`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_middle/lint/fn.in_external_macro.html
156+
[`in_external_macro`]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/struct.Span.html#method.in_external_macro
157157
[Span]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/struct.Span.html
158158
[SyntaxContext]: https://doc.rust-lang.org/stable/nightly-rustc/rustc_span/hygiene/struct.SyntaxContext.html

clippy_dev/src/update_lints.rs

+17-11
Original file line numberDiff line numberDiff line change
@@ -985,17 +985,23 @@ mod tests {
985985
Lint::new("incorrect_match", "group1", "\"abc\"", "module_name", Range::default()),
986986
];
987987
let mut expected: HashMap<String, Vec<Lint>> = HashMap::new();
988-
expected.insert("group1".to_string(), vec![
989-
Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name", Range::default()),
990-
Lint::new("incorrect_match", "group1", "\"abc\"", "module_name", Range::default()),
991-
]);
992-
expected.insert("group2".to_string(), vec![Lint::new(
993-
"should_assert_eq2",
994-
"group2",
995-
"\"abc\"",
996-
"module_name",
997-
Range::default(),
998-
)]);
988+
expected.insert(
989+
"group1".to_string(),
990+
vec![
991+
Lint::new("should_assert_eq", "group1", "\"abc\"", "module_name", Range::default()),
992+
Lint::new("incorrect_match", "group1", "\"abc\"", "module_name", Range::default()),
993+
],
994+
);
995+
expected.insert(
996+
"group2".to_string(),
997+
vec![Lint::new(
998+
"should_assert_eq2",
999+
"group2",
1000+
"\"abc\"",
1001+
"module_name",
1002+
Range::default(),
1003+
)],
1004+
);
9991005
assert_eq!(expected, Lint::by_lint_group(lints.into_iter()));
10001006
}
10011007
}

clippy_lints/src/almost_complete_range.rs

+2-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use clippy_utils::source::{trim_span, walk_span_to_context};
55
use rustc_ast::ast::{Expr, ExprKind, LitKind, Pat, PatKind, RangeEnd, RangeLimits};
66
use rustc_errors::Applicability;
77
use rustc_lint::{EarlyContext, EarlyLintPass, LintContext};
8-
use rustc_middle::lint::in_external_macro;
98
use rustc_session::impl_lint_pass;
109

1110
declare_clippy_lint! {
@@ -45,7 +44,7 @@ impl EarlyLintPass for AlmostCompleteRange {
4544
fn check_expr(&mut self, cx: &EarlyContext<'_>, e: &Expr) {
4645
if let ExprKind::Range(Some(start), Some(end), RangeLimits::HalfOpen) = &e.kind
4746
&& is_incomplete_range(start, end)
48-
&& !in_external_macro(cx.sess(), e.span)
47+
&& !e.span.in_external_macro(cx.sess().source_map())
4948
{
5049
span_lint_and_then(
5150
cx,
@@ -74,7 +73,7 @@ impl EarlyLintPass for AlmostCompleteRange {
7473
if let PatKind::Range(Some(start), Some(end), kind) = &p.kind
7574
&& matches!(kind.node, RangeEnd::Excluded)
7675
&& is_incomplete_range(start, end)
77-
&& !in_external_macro(cx.sess(), p.span)
76+
&& !p.span.in_external_macro(cx.sess().source_map())
7877
{
7978
span_lint_and_then(
8079
cx,

clippy_lints/src/arbitrary_source_item_ordering.rs

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_hir::{
99
Variant, VariantData,
1010
};
1111
use rustc_lint::{LateContext, LateLintPass, LintContext};
12-
use rustc_middle::lint::in_external_macro;
1312
use rustc_session::impl_lint_pass;
1413

1514
declare_clippy_lint! {
@@ -248,7 +247,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
248247
ItemKind::Enum(enum_def, _generics) if self.enable_ordering_for_enum => {
249248
let mut cur_v: Option<&Variant<'_>> = None;
250249
for variant in enum_def.variants {
251-
if in_external_macro(cx.sess(), variant.span) {
250+
if variant.span.in_external_macro(cx.sess().source_map()) {
252251
continue;
253252
}
254253

@@ -263,7 +262,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
263262
ItemKind::Struct(VariantData::Struct { fields, .. }, _generics) if self.enable_ordering_for_struct => {
264263
let mut cur_f: Option<&FieldDef<'_>> = None;
265264
for field in *fields {
266-
if in_external_macro(cx.sess(), field.span) {
265+
if field.span.in_external_macro(cx.sess().source_map()) {
267266
continue;
268267
}
269268

@@ -281,7 +280,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
281280
let mut cur_t: Option<&TraitItemRef> = None;
282281

283282
for item in *item_ref {
284-
if in_external_macro(cx.sess(), item.span) {
283+
if item.span.in_external_macro(cx.sess().source_map()) {
285284
continue;
286285
}
287286

@@ -304,7 +303,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
304303
let mut cur_t: Option<&ImplItemRef> = None;
305304

306305
for item in trait_impl.items {
307-
if in_external_macro(cx.sess(), item.span) {
306+
if item.span.in_external_macro(cx.sess().source_map()) {
308307
continue;
309308
}
310309

@@ -348,7 +347,7 @@ impl<'tcx> LateLintPass<'tcx> for ArbitrarySourceItemOrdering {
348347
// as no sorting by source map/line of code has to be applied.
349348
//
350349
for item in items {
351-
if in_external_macro(cx.sess(), item.span) {
350+
if item.span.in_external_macro(cx.sess().source_map()) {
352351
continue;
353352
}
354353

clippy_lints/src/as_conversions.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::is_from_proc_macro;
33
use rustc_hir::{Expr, ExprKind};
44
use rustc_lint::{LateContext, LateLintPass, LintContext};
5-
use rustc_middle::lint::in_external_macro;
65
use rustc_session::declare_lint_pass;
76

87
declare_clippy_lint! {
@@ -49,7 +48,7 @@ declare_lint_pass!(AsConversions => [AS_CONVERSIONS]);
4948
impl<'tcx> LateLintPass<'tcx> for AsConversions {
5049
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &Expr<'tcx>) {
5150
if let ExprKind::Cast(_, _) = expr.kind
52-
&& !in_external_macro(cx.sess(), expr.span)
51+
&& !expr.span.in_external_macro(cx.sess().source_map())
5352
&& !is_from_proc_macro(cx, expr)
5453
{
5554
#[expect(clippy::collapsible_span_lint_calls, reason = "rust-clippy#7797")]

clippy_lints/src/attrs/allow_attributes.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@ use clippy_utils::is_from_proc_macro;
44
use rustc_ast::{AttrStyle, Attribute};
55
use rustc_errors::Applicability;
66
use rustc_lint::{EarlyContext, LintContext};
7-
use rustc_middle::lint::in_external_macro;
87

98
// Separate each crate's features.
109
pub fn check<'cx>(cx: &EarlyContext<'cx>, attr: &'cx Attribute) {
11-
if !in_external_macro(cx.sess(), attr.span)
10+
if !attr.span.in_external_macro(cx.sess().source_map())
1211
&& let AttrStyle::Outer = attr.style
1312
&& let Some(ident) = attr.ident()
1413
&& !is_from_proc_macro(cx, attr)

clippy_lints/src/attrs/allow_attributes_without_reason.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use clippy_utils::diagnostics::span_lint_and_then;
33
use clippy_utils::is_from_proc_macro;
44
use rustc_ast::{MetaItemInner, MetaItemKind};
55
use rustc_lint::{EarlyContext, LintContext};
6-
use rustc_middle::lint::in_external_macro;
76
use rustc_span::sym;
87
use rustc_span::symbol::Symbol;
98

@@ -17,7 +16,7 @@ pub(super) fn check<'cx>(cx: &EarlyContext<'cx>, name: Symbol, items: &[MetaItem
1716
}
1817

1918
// Check if the attribute is in an external macro and therefore out of the developer's control
20-
if in_external_macro(cx.sess(), attr.span) || is_from_proc_macro(cx, attr) {
19+
if attr.span.in_external_macro(cx.sess().source_map()) || is_from_proc_macro(cx, attr) {
2120
return;
2221
}
2322

clippy_lints/src/attrs/useless_attribute.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,13 @@ use clippy_utils::source::{SpanRangeExt, first_line_of_span};
55
use rustc_ast::{Attribute, Item, ItemKind};
66
use rustc_errors::Applicability;
77
use rustc_lint::{EarlyContext, LintContext};
8-
use rustc_middle::lint::in_external_macro;
98
use rustc_span::sym;
109

1110
pub(super) fn check(cx: &EarlyContext<'_>, item: &Item, attrs: &[Attribute]) {
1211
let skip_unused_imports = attrs.iter().any(|attr| attr.has_name(sym::macro_use));
1312

1413
for attr in attrs {
15-
if in_external_macro(cx.sess(), attr.span) {
14+
if attr.span.in_external_macro(cx.sess().source_map()) {
1615
return;
1716
}
1817
if let Some(lint_list) = &attr.meta_item_list() {

clippy_lints/src/blocks_in_conditions.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ use clippy_utils::{higher, is_from_proc_macro};
44
use rustc_errors::Applicability;
55
use rustc_hir::{BlockCheckMode, Expr, ExprKind, MatchSource};
66
use rustc_lint::{LateContext, LateLintPass, LintContext};
7-
use rustc_middle::lint::in_external_macro;
87
use rustc_session::declare_lint_pass;
98

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

5554
impl<'tcx> LateLintPass<'tcx> for BlocksInConditions {
5655
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
57-
if in_external_macro(cx.sess(), expr.span) {
56+
if expr.span.in_external_macro(cx.sess().source_map()) {
5857
return;
5958
}
6059

clippy_lints/src/box_default.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_hir::def::Res;
77
use rustc_hir::intravisit::{InferKind, Visitor, VisitorExt, walk_ty};
88
use rustc_hir::{AmbigArg, Block, Expr, ExprKind, HirId, LetStmt, Node, QPath, Ty, TyKind};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
10-
use rustc_middle::lint::in_external_macro;
1110
use rustc_session::declare_lint_pass;
1211
use rustc_span::{Span, sym};
1312

@@ -50,7 +49,7 @@ impl LateLintPass<'_> for BoxDefault {
5049
// This is the `T::default()` (or default equivalent) of `Box::new(T::default())`
5150
&& let ExprKind::Call(arg_path, _) = arg.kind
5251
// And we are not in a foreign crate's macro
53-
&& !in_external_macro(cx.sess(), expr.span)
52+
&& !expr.span.in_external_macro(cx.sess().source_map())
5453
// And the argument expression has the same context as the outer call expression
5554
// or that we are inside a `vec!` macro expansion
5655
&& (expr.span.eq_ctxt(arg.span) || is_local_vec_expn(cx, arg, expr))

clippy_lints/src/casts/mod.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ use clippy_utils::is_hir_ty_cfg_dependant;
2929
use clippy_utils::msrvs::{self, Msrv};
3030
use rustc_hir::{Expr, ExprKind};
3131
use rustc_lint::{LateContext, LateLintPass, LintContext};
32-
use rustc_middle::lint::in_external_macro;
3332
use rustc_session::impl_lint_pass;
3433

3534
declare_clippy_lint! {
@@ -796,7 +795,7 @@ impl_lint_pass!(Casts => [
796795

797796
impl<'tcx> LateLintPass<'tcx> for Casts {
798797
fn check_expr(&mut self, cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) {
799-
if in_external_macro(cx.sess(), expr.span) {
798+
if expr.span.in_external_macro(cx.sess().source_map()) {
800799
return;
801800
}
802801

clippy_lints/src/casts/unnecessary_cast.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use rustc_errors::Applicability;
88
use rustc_hir::def::{DefKind, Res};
99
use rustc_hir::{Expr, ExprKind, Lit, Node, Path, QPath, TyKind, UnOp};
1010
use rustc_lint::{LateContext, LintContext};
11-
use rustc_middle::lint::in_external_macro;
1211
use rustc_middle::ty::{self, FloatTy, InferTy, Ty};
1312
use std::ops::ControlFlow;
1413

@@ -142,7 +141,7 @@ pub(super) fn check<'tcx>(
142141
}
143142
}
144143

145-
if cast_from.kind() == cast_to.kind() && !in_external_macro(cx.sess(), expr.span) {
144+
if cast_from.kind() == cast_to.kind() && !expr.span.in_external_macro(cx.sess().source_map()) {
146145
if let Some(id) = path_to_local(cast_expr)
147146
&& !cx.tcx.hir().span(id).eq_ctxt(cast_expr.span)
148147
{

clippy_lints/src/checked_conversions.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ use clippy_utils::{SpanlessEq, is_in_const_context, is_integer_literal};
66
use rustc_errors::Applicability;
77
use rustc_hir::{BinOpKind, Expr, ExprKind, QPath, TyKind};
88
use rustc_lint::{LateContext, LateLintPass, LintContext};
9-
use rustc_middle::lint::in_external_macro;
109
use rustc_session::impl_lint_pass;
1110

1211
declare_clippy_lint! {
@@ -64,7 +63,7 @@ impl LateLintPass<'_> for CheckedConversions {
6463
},
6564
_ => return,
6665
}
67-
&& !in_external_macro(cx.sess(), item.span)
66+
&& !item.span.in_external_macro(cx.sess().source_map())
6867
&& !is_in_const_context(cx)
6968
&& self.msrv.meets(msrvs::TRY_FROM)
7069
&& let Some(cv) = match op2 {

clippy_lints/src/ctfe.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ impl<'tcx> LateLintPass<'tcx> for ClippyCtfe {
2121
_: Span,
2222
defid: LocalDefId,
2323
) {
24-
cx.tcx.ensure().mir_drops_elaborated_and_const_checked(defid); // Lint
24+
cx.tcx.ensure_ok().mir_drops_elaborated_and_const_checked(defid); // Lint
2525
}
2626
}

clippy_lints/src/dbg_macro.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ use rustc_data_structures::fx::FxHashSet;
77
use rustc_errors::Applicability;
88
use rustc_hir::{Expr, ExprKind, Node};
99
use rustc_lint::{LateContext, LateLintPass, LintContext};
10-
use rustc_middle::lint::in_external_macro;
1110
use rustc_session::impl_lint_pass;
1211
use rustc_span::{Span, SyntaxContext, sym};
1312

@@ -60,7 +59,7 @@ impl LateLintPass<'_> for DbgMacro {
6059

6160
if cur_syntax_ctxt != self.prev_ctxt &&
6261
let Some(macro_call) = first_dbg_macro_in_expansion(cx, expr.span) &&
63-
!in_external_macro(cx.sess(), macro_call.span) &&
62+
!macro_call.span.in_external_macro(cx.sess().source_map()) &&
6463
self.checked_dbg_call_site.insert(macro_call.span) &&
6564
// allows `dbg!` in test code if allow-dbg-in-test is set to true in clippy.toml
6665
!(self.allow_dbg_in_tests && is_in_test(cx.tcx, expr.hir_id))

clippy_lints/src/default_numeric_fallback.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ use rustc_hir::{
99
StructTailExpr,
1010
};
1111
use rustc_lint::{LateContext, LateLintPass, LintContext};
12-
use rustc_middle::lint::in_external_macro;
1312
use rustc_middle::ty::{self, FloatTy, IntTy, PolyFnSig, Ty};
1413
use rustc_session::declare_lint_pass;
1514
use std::iter;
@@ -86,7 +85,7 @@ impl<'a, 'tcx> NumericFallbackVisitor<'a, 'tcx> {
8685

8786
/// Check whether a passed literal has potential to cause fallback or not.
8887
fn check_lit(&self, lit: &Lit, lit_ty: Ty<'tcx>, emit_hir_id: HirId) {
89-
if !in_external_macro(self.cx.sess(), lit.span)
88+
if !lit.span.in_external_macro(self.cx.sess().source_map())
9089
&& matches!(self.ty_bounds.last(), Some(ExplicitTyBound(false)))
9190
&& matches!(
9291
lit.node,

0 commit comments

Comments
 (0)