Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix find_format_arg_expr when incremental compilation is enabled #10980

Merged
merged 1 commit into from
Jun 19, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions clippy_utils/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use rustc_hir::{self as hir, Expr, ExprKind, HirId, Node, QPath};
use rustc_lint::LateContext;
use rustc_span::def_id::DefId;
use rustc_span::hygiene::{self, MacroKind, SyntaxContext};
use rustc_span::{sym, BytePos, ExpnData, ExpnId, ExpnKind, Span, Symbol};
use rustc_span::{sym, BytePos, ExpnData, ExpnId, ExpnKind, Span, SpanData, Symbol};
use std::cell::RefCell;
use std::ops::ControlFlow;
use std::sync::atomic::{AtomicBool, Ordering};
Expand Down Expand Up @@ -415,8 +415,18 @@ pub fn find_format_arg_expr<'hir, 'ast>(
start: &'hir Expr<'hir>,
target: &'ast FormatArgument,
) -> Result<&'hir rustc_hir::Expr<'hir>, &'ast rustc_ast::Expr> {
let SpanData {
lo,
hi,
ctxt,
parent: _,
} = target.expr.span.data();

for_each_expr(start, |expr| {
if expr.span == target.expr.span {
// When incremental compilation is enabled spans gain a parent during AST to HIR lowering,
// since we're comparing an AST span to a HIR one we need to ignore the parent field
Comment on lines +426 to +427
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh, TIL 👀

let data = expr.span.data();
if data.lo == lo && data.hi == hi && data.ctxt == ctxt {
ControlFlow::Break(expr)
} else {
ControlFlow::Continue(())
Expand Down
9 changes: 9 additions & 0 deletions tests/ui/to_string_in_format_args_incremental.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@run-rustfix
//@compile-flags: -C incremental=target/debug/test/incr

// see https://github.com/rust-lang/rust-clippy/issues/10969

fn main() {
let s = "Hello, world!";
println!("{}", s);
}
9 changes: 9 additions & 0 deletions tests/ui/to_string_in_format_args_incremental.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
//@run-rustfix
//@compile-flags: -C incremental=target/debug/test/incr

// see https://github.com/rust-lang/rust-clippy/issues/10969

fn main() {
let s = "Hello, world!";
println!("{}", s.to_string());
}
10 changes: 10 additions & 0 deletions tests/ui/to_string_in_format_args_incremental.stderr
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
error: `to_string` applied to a type that implements `Display` in `println!` args
--> $DIR/to_string_in_format_args_incremental.rs:8:21
|
LL | println!("{}", s.to_string());
| ^^^^^^^^^^^^ help: remove this
|
= note: `-D clippy::to-string-in-format-args` implied by `-D warnings`

error: aborting due to previous error