Skip to content

Commit deadc25

Browse files
committed
Factor out differing_macro_contexts
1 parent 66a83d3 commit deadc25

File tree

9 files changed

+24
-31
lines changed

9 files changed

+24
-31
lines changed

clippy_lints/src/blocks_in_if_conditions.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg};
2+
use clippy_utils::get_parent_expr;
23
use clippy_utils::higher;
34
use clippy_utils::source::snippet_block_with_applicability;
45
use clippy_utils::ty::implements_trait;
5-
use clippy_utils::{differing_macro_contexts, get_parent_expr};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::intravisit::{walk_expr, Visitor};
@@ -97,7 +97,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
9797
if let Some(ex) = &block.expr {
9898
// don't dig into the expression here, just suggest that they remove
9999
// the block
100-
if expr.span.from_expansion() || differing_macro_contexts(expr.span, ex.span) {
100+
if expr.span.from_expansion() || ex.span.from_expansion() {
101101
return;
102102
}
103103
let mut applicability = Applicability::MachineApplicable;
@@ -122,7 +122,7 @@ impl<'tcx> LateLintPass<'tcx> for BlocksInIfConditions {
122122
}
123123
} else {
124124
let span = block.expr.as_ref().map_or_else(|| block.stmts[0].span, |e| e.span);
125-
if span.from_expansion() || differing_macro_contexts(expr.span, span) {
125+
if span.from_expansion() || expr.span.from_expansion() {
126126
return;
127127
}
128128
// move block higher

clippy_lints/src/formatting.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::{span_lint_and_help, span_lint_and_note};
2-
use clippy_utils::differing_macro_contexts;
32
use clippy_utils::source::snippet_opt;
43
use if_chain::if_chain;
54
use rustc_ast::ast::{BinOpKind, Block, Expr, ExprKind, StmtKind, UnOp};
@@ -135,7 +134,7 @@ impl EarlyLintPass for Formatting {
135134
/// Implementation of the `SUSPICIOUS_ASSIGNMENT_FORMATTING` lint.
136135
fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
137136
if let ExprKind::Assign(ref lhs, ref rhs, _) = expr.kind {
138-
if !differing_macro_contexts(lhs.span, rhs.span) && !lhs.span.from_expansion() {
137+
if !lhs.span.from_expansion() && !rhs.span.from_expansion() {
139138
let eq_span = lhs.span.between(rhs.span);
140139
if let ExprKind::Unary(op, ref sub_rhs) = rhs.kind {
141140
if let Some(eq_snippet) = snippet_opt(cx, eq_span) {
@@ -165,7 +164,7 @@ fn check_assign(cx: &EarlyContext<'_>, expr: &Expr) {
165164
fn check_unop(cx: &EarlyContext<'_>, expr: &Expr) {
166165
if_chain! {
167166
if let ExprKind::Binary(ref binop, ref lhs, ref rhs) = expr.kind;
168-
if !differing_macro_contexts(lhs.span, rhs.span) && !lhs.span.from_expansion();
167+
if !lhs.span.from_expansion() && !rhs.span.from_expansion();
169168
// span between BinOp LHS and RHS
170169
let binop_span = lhs.span.between(rhs.span);
171170
// if RHS is an UnOp
@@ -206,8 +205,8 @@ fn check_else(cx: &EarlyContext<'_>, expr: &Expr) {
206205
if_chain! {
207206
if let ExprKind::If(_, then, Some(else_)) = &expr.kind;
208207
if is_block(else_) || is_if(else_);
209-
if !differing_macro_contexts(then.span, else_.span);
210-
if !then.span.from_expansion() && !in_external_macro(cx.sess(), expr.span);
208+
if !then.span.from_expansion() && !else_.span.from_expansion();
209+
if !in_external_macro(cx.sess(), expr.span);
211210

212211
// workaround for rust-lang/rust#43081
213212
if expr.span.lo().0 != 0 && expr.span.hi().0 != 0;
@@ -268,7 +267,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
268267
for element in array {
269268
if_chain! {
270269
if let ExprKind::Binary(ref op, ref lhs, _) = element.kind;
271-
if has_unary_equivalent(op.node) && !differing_macro_contexts(lhs.span, op.span);
270+
if has_unary_equivalent(op.node) && lhs.span.ctxt() == op.span.ctxt();
272271
let space_span = lhs.span.between(op.span);
273272
if let Some(space_snippet) = snippet_opt(cx, space_span);
274273
let lint_span = lhs.span.with_lo(lhs.span.hi());
@@ -291,8 +290,7 @@ fn check_array(cx: &EarlyContext<'_>, expr: &Expr) {
291290

292291
fn check_missing_else(cx: &EarlyContext<'_>, first: &Expr, second: &Expr) {
293292
if_chain! {
294-
if !differing_macro_contexts(first.span, second.span);
295-
if !first.span.from_expansion();
293+
if !first.span.from_expansion() && !second.span.from_expansion();
296294
if let ExprKind::If(cond_expr, ..) = &first.kind;
297295
if is_block(second) || is_if(second);
298296

clippy_lints/src/implicit_hasher.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_typeck::hir_ty_to_ty;
1717
use if_chain::if_chain;
1818

1919
use clippy_utils::diagnostics::{multispan_sugg, span_lint_and_then};
20-
use clippy_utils::differing_macro_contexts;
2120
use clippy_utils::source::{snippet, snippet_opt};
2221
use clippy_utils::ty::is_type_diagnostic_item;
2322

@@ -123,7 +122,7 @@ impl<'tcx> LateLintPass<'tcx> for ImplicitHasher {
123122
vis.visit_ty(impl_.self_ty);
124123

125124
for target in &vis.found {
126-
if differing_macro_contexts(item.span, target.span()) {
125+
if item.span.ctxt() != target.span().ctxt() {
127126
return;
128127
}
129128

clippy_lints/src/methods/option_map_unwrap_or.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
2-
use clippy_utils::differing_macro_contexts;
32
use clippy_utils::source::snippet_with_applicability;
43
use clippy_utils::ty::is_copy;
54
use clippy_utils::ty::is_type_diagnostic_item;
@@ -48,7 +47,7 @@ pub(super) fn check<'tcx>(
4847
}
4948
}
5049

51-
if differing_macro_contexts(unwrap_arg.span, map_span) {
50+
if unwrap_arg.span.ctxt() != map_span.ctxt() {
5251
return;
5352
}
5453

clippy_lints/src/swap.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use clippy_utils::diagnostics::{span_lint_and_sugg, span_lint_and_then};
22
use clippy_utils::source::snippet_with_applicability;
33
use clippy_utils::sugg::Sugg;
44
use clippy_utils::ty::is_type_diagnostic_item;
5-
use clippy_utils::{can_mut_borrow_both, differing_macro_contexts, eq_expr_value, std_or_core};
5+
use clippy_utils::{can_mut_borrow_both, eq_expr_value, std_or_core};
66
use if_chain::if_chain;
77
use rustc_errors::Applicability;
88
use rustc_hir::{BinOpKind, Block, Expr, ExprKind, PatKind, QPath, Stmt, StmtKind};
@@ -172,7 +172,7 @@ fn check_suspicious_swap(cx: &LateContext<'_>, block: &Block<'_>) {
172172
if_chain! {
173173
if let StmtKind::Semi(first) = w[0].kind;
174174
if let StmtKind::Semi(second) = w[1].kind;
175-
if !differing_macro_contexts(first.span, second.span);
175+
if first.span.ctxt() == second.span.ctxt();
176176
if let ExprKind::Assign(lhs0, rhs0, _) = first.kind;
177177
if let ExprKind::Assign(lhs1, rhs1, _) = second.kind;
178178
if eq_expr_value(cx, lhs0, rhs1);

clippy_lints/src/unwrap.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use clippy_utils::diagnostics::span_lint_and_then;
22
use clippy_utils::higher;
33
use clippy_utils::ty::is_type_diagnostic_item;
4-
use clippy_utils::{differing_macro_contexts, path_to_local, usage::is_potentially_mutated};
4+
use clippy_utils::{path_to_local, usage::is_potentially_mutated};
55
use if_chain::if_chain;
66
use rustc_errors::Applicability;
77
use rustc_hir::intravisit::{walk_expr, walk_fn, FnKind, Visitor};
@@ -238,8 +238,9 @@ impl<'a, 'tcx> Visitor<'tcx> for UnwrappableVariablesVisitor<'a, 'tcx> {
238238
if let Some(unwrappable) = self.unwrappables.iter()
239239
.find(|u| u.local_id == id);
240240
// Span contexts should not differ with the conditional branch
241-
if !differing_macro_contexts(unwrappable.branch.span, expr.span);
242-
if !differing_macro_contexts(unwrappable.branch.span, unwrappable.check.span);
241+
let span_ctxt = expr.span.ctxt();
242+
if unwrappable.branch.span.ctxt() == span_ctxt;
243+
if unwrappable.check.span.ctxt() == span_ctxt;
243244
then {
244245
if call_to_unwrap == unwrappable.safe_to_unwrap {
245246
let is_entire_condition = unwrappable.is_entire_condition;

clippy_utils/src/hir_utils.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use crate::consts::{constant_context, constant_simple};
2-
use crate::differing_macro_contexts;
32
use crate::source::snippet_opt;
43
use rustc_ast::ast::InlineAsmTemplatePiece;
54
use rustc_data_structures::fx::FxHasher;
@@ -186,7 +185,7 @@ impl HirEqInterExpr<'_, '_, '_> {
186185

187186
#[allow(clippy::similar_names)]
188187
pub fn eq_expr(&mut self, left: &Expr<'_>, right: &Expr<'_>) -> bool {
189-
if !self.inner.allow_side_effects && differing_macro_contexts(left.span, right.span) {
188+
if !self.inner.allow_side_effects && left.span.ctxt() != right.span.ctxt() {
190189
return false;
191190
}
192191

clippy_utils/src/lib.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,13 +134,6 @@ macro_rules! extract_msrv_attr {
134134
};
135135
}
136136

137-
/// Returns `true` if the two spans come from differing expansions (i.e., one is
138-
/// from a macro and one isn't).
139-
#[must_use]
140-
pub fn differing_macro_contexts(lhs: Span, rhs: Span) -> bool {
141-
rhs.ctxt() != lhs.ctxt()
142-
}
143-
144137
/// If the given expression is a local binding, find the initializer expression.
145138
/// If that initializer expression is another local binding, find its initializer again.
146139
/// This process repeats as long as possible (but usually no more than once). Initializer

doc/common_tools_writing_lints.md

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,11 @@ Use the following functions to deal with macros:
235235
assert_eq!(in_external_macro(cx.sess(), match_span), true);
236236
```
237237

238-
- `differing_macro_contexts()`: returns true if the two given spans are not from the same context
238+
- `span.ctxt()`: the span's context represents whether it is from expansion, and if so, what expanded it
239+
240+
One thing `SpanContext` is useful for is to check if two spans are in the same context. For example,
241+
in `a == b`, `a` and `b` have the same context. In a `macro_rules!` with `a == $b`, `$b` is expanded to some
242+
expression with a different context from `a`.
239243

240244
```rust
241245
macro_rules! m {
@@ -252,7 +256,7 @@ Use the following functions to deal with macros:
252256
// These spans are not from the same context
253257
// x.is_some() is from inside the macro
254258
// x.unwrap() is from outside the macro
255-
assert_eq!(differing_macro_contexts(x_is_some_span, x_unwrap_span), true);
259+
assert_eq!(x_is_some_span.ctxt(), x_unwrap_span.ctxt());
256260
```
257261

258262
[TyS]: https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyS.html

0 commit comments

Comments
 (0)