Skip to content

Commit 26e78e7

Browse files
committed
Fix find_format_arg_expr when incremental compilation is enabled
1 parent 8c8ff5f commit 26e78e7

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)