Skip to content

Commit ebcbba9

Browse files
committed
Auto merge of #10980 - Alexendoo:format-args-incremental-spans, r=dswij
Fix `find_format_arg_expr` when incremental compilation is enabled Fixes #10969 [`lower_span`](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_ast_lowering/struct.LoweringContext.html#method.lower_span) gives AST spans a [parent](https://doc.rust-lang.org/nightly/nightly-rustc/rustc_span/struct.SpanData.html#structfield.parent) when lowering to the HIR. That meant the `==` span comparison would return false even though the spans are pointing to the same thing. We now ignore the parent when comparing the spans Debugging this was quite the puzzle, because the parent is not included in the debug output of `Span`s or `SpanData` 😬 changelog: none
2 parents 06b444b + 26e78e7 commit ebcbba9

File tree

4 files changed

+40
-2
lines changed

4 files changed

+40
-2
lines changed

clippy_utils/src/macros.rs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::{self as hir, Expr, ExprKind, HirId, Node, QPath};
99
use rustc_lint::LateContext;
1010
use rustc_span::def_id::DefId;
1111
use rustc_span::hygiene::{self, MacroKind, SyntaxContext};
12-
use rustc_span::{sym, BytePos, ExpnData, ExpnId, ExpnKind, Span, Symbol};
12+
use rustc_span::{sym, BytePos, ExpnData, ExpnId, ExpnKind, Span, SpanData, Symbol};
1313
use std::cell::RefCell;
1414
use std::ops::ControlFlow;
1515
use std::sync::atomic::{AtomicBool, Ordering};
@@ -415,8 +415,18 @@ pub fn find_format_arg_expr<'hir, 'ast>(
415415
start: &'hir Expr<'hir>,
416416
target: &'ast FormatArgument,
417417
) -> Result<&'hir rustc_hir::Expr<'hir>, &'ast rustc_ast::Expr> {
418+
let SpanData {
419+
lo,
420+
hi,
421+
ctxt,
422+
parent: _,
423+
} = target.expr.span.data();
424+
418425
for_each_expr(start, |expr| {
419-
if expr.span == target.expr.span {
426+
// When incremental compilation is enabled spans gain a parent during AST to HIR lowering,
427+
// since we're comparing an AST span to a HIR one we need to ignore the parent field
428+
let data = expr.span.data();
429+
if data.lo == lo && data.hi == hi && data.ctxt == ctxt {
420430
ControlFlow::Break(expr)
421431
} else {
422432
ControlFlow::Continue(())
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@run-rustfix
2+
//@compile-flags: -C incremental=target/debug/test/incr
3+
4+
// see https://github.com/rust-lang/rust-clippy/issues/10969
5+
6+
fn main() {
7+
let s = "Hello, world!";
8+
println!("{}", s);
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
//@run-rustfix
2+
//@compile-flags: -C incremental=target/debug/test/incr
3+
4+
// see https://github.com/rust-lang/rust-clippy/issues/10969
5+
6+
fn main() {
7+
let s = "Hello, world!";
8+
println!("{}", s.to_string());
9+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
error: `to_string` applied to a type that implements `Display` in `println!` args
2+
--> $DIR/to_string_in_format_args_incremental.rs:8:21
3+
|
4+
LL | println!("{}", s.to_string());
5+
| ^^^^^^^^^^^^ help: remove this
6+
|
7+
= note: `-D clippy::to-string-in-format-args` implied by `-D warnings`
8+
9+
error: aborting due to previous error
10+

0 commit comments

Comments
 (0)