Skip to content

Commit 4bae06d

Browse files
committed
Use source callsite in FormatArgsExpn::inputs_span
1 parent 699ee5e commit 4bae06d

9 files changed

+98
-32
lines changed

clippy_utils/src/macros.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_hir::intravisit::Visitor;
99
use rustc_hir::{self as hir, Expr, ExprKind, HirId, Node, QPath};
1010
use rustc_lint::LateContext;
1111
use rustc_span::def_id::DefId;
12-
use rustc_span::hygiene::{MacroKind, SyntaxContext};
12+
use rustc_span::hygiene::{self, MacroKind, SyntaxContext};
1313
use rustc_span::{sym, ExpnData, ExpnId, ExpnKind, Span, Symbol};
1414
use std::ops::ControlFlow;
1515

@@ -306,6 +306,7 @@ fn is_assert_arg(cx: &LateContext<'_>, expr: &Expr<'_>, assert_expn: ExpnId) ->
306306
}
307307

308308
/// A parsed `format_args!` expansion
309+
#[derive(Debug)]
309310
pub struct FormatArgsExpn<'tcx> {
310311
/// Span of the first argument, the format string
311312
pub format_string_span: Span,
@@ -465,11 +466,13 @@ impl<'tcx> FormatArgsExpn<'tcx> {
465466
.collect()
466467
}
467468

468-
/// Span of all inputs
469+
/// Source callsite span of all inputs
469470
pub fn inputs_span(&self) -> Span {
470471
match *self.value_args {
471472
[] => self.format_string_span,
472-
[.., last] => self.format_string_span.to(last.span),
473+
[.., last] => self
474+
.format_string_span
475+
.to(hygiene::walk_chain(last.span, self.format_string_span.ctxt())),
473476
}
474477
}
475478
}

tests/ui/expect_fun_call.fixed

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
/// Checks implementation of the `EXPECT_FUN_CALL` lint
77

8+
macro_rules! one {
9+
() => {
10+
1
11+
};
12+
}
13+
814
fn main() {
915
struct Foo;
1016

@@ -31,6 +37,9 @@ fn main() {
3137
let with_none_and_as_str: Option<i32> = None;
3238
with_none_and_as_str.unwrap_or_else(|| panic!("Error {}: fake error", error_code));
3339

40+
let with_none_and_format_with_macro: Option<i32> = None;
41+
with_none_and_format_with_macro.unwrap_or_else(|| panic!("Error {}: fake error", one!()));
42+
3443
let with_ok: Result<(), ()> = Ok(());
3544
with_ok.expect("error");
3645

tests/ui/expect_fun_call.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@
55

66
/// Checks implementation of the `EXPECT_FUN_CALL` lint
77
8+
macro_rules! one {
9+
() => {
10+
1
11+
};
12+
}
13+
814
fn main() {
915
struct Foo;
1016

@@ -31,6 +37,9 @@ fn main() {
3137
let with_none_and_as_str: Option<i32> = None;
3238
with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
3339

40+
let with_none_and_format_with_macro: Option<i32> = None;
41+
with_none_and_format_with_macro.expect(format!("Error {}: fake error", one!()).as_str());
42+
3443
let with_ok: Result<(), ()> = Ok(());
3544
with_ok.expect("error");
3645

tests/ui/expect_fun_call.stderr

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,76 +1,82 @@
11
error: use of `expect` followed by a function call
2-
--> $DIR/expect_fun_call.rs:29:26
2+
--> $DIR/expect_fun_call.rs:35:26
33
|
44
LL | with_none_and_format.expect(&format!("Error {}: fake error", error_code));
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))`
66
|
77
= note: `-D clippy::expect-fun-call` implied by `-D warnings`
88

99
error: use of `expect` followed by a function call
10-
--> $DIR/expect_fun_call.rs:32:26
10+
--> $DIR/expect_fun_call.rs:38:26
1111
|
1212
LL | with_none_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", error_code))`
1414

1515
error: use of `expect` followed by a function call
16-
--> $DIR/expect_fun_call.rs:42:25
16+
--> $DIR/expect_fun_call.rs:41:37
17+
|
18+
LL | with_none_and_format_with_macro.expect(format!("Error {}: fake error", one!()).as_str());
19+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("Error {}: fake error", one!()))`
20+
21+
error: use of `expect` followed by a function call
22+
--> $DIR/expect_fun_call.rs:51:25
1723
|
1824
LL | with_err_and_format.expect(&format!("Error {}: fake error", error_code));
1925
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`
2026

2127
error: use of `expect` followed by a function call
22-
--> $DIR/expect_fun_call.rs:45:25
28+
--> $DIR/expect_fun_call.rs:54:25
2329
|
2430
LL | with_err_and_as_str.expect(format!("Error {}: fake error", error_code).as_str());
2531
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|_| panic!("Error {}: fake error", error_code))`
2632

2733
error: use of `expect` followed by a function call
28-
--> $DIR/expect_fun_call.rs:57:17
34+
--> $DIR/expect_fun_call.rs:66:17
2935
|
3036
LL | Some("foo").expect(format!("{} {}", 1, 2).as_ref());
3137
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("{} {}", 1, 2))`
3238

3339
error: use of `expect` followed by a function call
34-
--> $DIR/expect_fun_call.rs:78:21
40+
--> $DIR/expect_fun_call.rs:87:21
3541
|
3642
LL | Some("foo").expect(&get_string());
3743
| ^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!("{}", get_string()) })`
3844

3945
error: use of `expect` followed by a function call
40-
--> $DIR/expect_fun_call.rs:79:21
46+
--> $DIR/expect_fun_call.rs:88:21
4147
|
4248
LL | Some("foo").expect(get_string().as_ref());
4349
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!("{}", get_string()) })`
4450

4551
error: use of `expect` followed by a function call
46-
--> $DIR/expect_fun_call.rs:80:21
52+
--> $DIR/expect_fun_call.rs:89:21
4753
|
4854
LL | Some("foo").expect(get_string().as_str());
4955
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!("{}", get_string()) })`
5056

5157
error: use of `expect` followed by a function call
52-
--> $DIR/expect_fun_call.rs:82:21
58+
--> $DIR/expect_fun_call.rs:91:21
5359
|
5460
LL | Some("foo").expect(get_static_str());
5561
| ^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!("{}", get_static_str()) })`
5662

5763
error: use of `expect` followed by a function call
58-
--> $DIR/expect_fun_call.rs:83:21
64+
--> $DIR/expect_fun_call.rs:92:21
5965
|
6066
LL | Some("foo").expect(get_non_static_str(&0));
6167
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| { panic!("{}", get_non_static_str(&0).to_string()) })`
6268

6369
error: use of `expect` followed by a function call
64-
--> $DIR/expect_fun_call.rs:87:16
70+
--> $DIR/expect_fun_call.rs:96:16
6571
|
6672
LL | Some(true).expect(&format!("key {}, {}", 1, 2));
6773
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("key {}, {}", 1, 2))`
6874

6975
error: use of `expect` followed by a function call
70-
--> $DIR/expect_fun_call.rs:93:17
76+
--> $DIR/expect_fun_call.rs:102:17
7177
|
7278
LL | opt_ref.expect(&format!("{:?}", opt_ref));
7379
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: try this: `unwrap_or_else(|| panic!("{:?}", opt_ref))`
7480

75-
error: aborting due to 12 previous errors
81+
error: aborting due to 13 previous errors
7682

tests/ui/manual_assert.edition2018.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#![warn(clippy::manual_assert)]
77
#![allow(clippy::nonminimal_bool)]
88

9+
macro_rules! one {
10+
() => {
11+
1
12+
};
13+
}
14+
915
fn main() {
1016
let a = vec![1, 2, 3];
1117
let c = Some(2);
@@ -42,4 +48,5 @@ fn main() {
4248
assert!(!(a.is_empty() && !b.is_empty()), "panic3");
4349
assert!(!(b.is_empty() || a.is_empty()), "panic4");
4450
assert!(!(a.is_empty() || !b.is_empty()), "panic5");
51+
assert!(!a.is_empty(), "with expansion {}", one!());
4552
}
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: only a `panic!` in `if`-then statement
2-
--> $DIR/manual_assert.rs:24:5
2+
--> $DIR/manual_assert.rs:30:5
33
|
44
LL | / if !a.is_empty() {
55
LL | | panic!("qaqaq{:?}", a);
@@ -9,52 +9,60 @@ LL | | }
99
= note: `-D clippy::manual-assert` implied by `-D warnings`
1010

1111
error: only a `panic!` in `if`-then statement
12-
--> $DIR/manual_assert.rs:27:5
12+
--> $DIR/manual_assert.rs:33:5
1313
|
1414
LL | / if !a.is_empty() {
1515
LL | | panic!("qwqwq");
1616
LL | | }
1717
| |_____^ help: try: `assert!(a.is_empty(), "qwqwq");`
1818

1919
error: only a `panic!` in `if`-then statement
20-
--> $DIR/manual_assert.rs:44:5
20+
--> $DIR/manual_assert.rs:50:5
2121
|
2222
LL | / if b.is_empty() {
2323
LL | | panic!("panic1");
2424
LL | | }
2525
| |_____^ help: try: `assert!(!b.is_empty(), "panic1");`
2626

2727
error: only a `panic!` in `if`-then statement
28-
--> $DIR/manual_assert.rs:47:5
28+
--> $DIR/manual_assert.rs:53:5
2929
|
3030
LL | / if b.is_empty() && a.is_empty() {
3131
LL | | panic!("panic2");
3232
LL | | }
3333
| |_____^ help: try: `assert!(!(b.is_empty() && a.is_empty()), "panic2");`
3434

3535
error: only a `panic!` in `if`-then statement
36-
--> $DIR/manual_assert.rs:50:5
36+
--> $DIR/manual_assert.rs:56:5
3737
|
3838
LL | / if a.is_empty() && !b.is_empty() {
3939
LL | | panic!("panic3");
4040
LL | | }
4141
| |_____^ help: try: `assert!(!(a.is_empty() && !b.is_empty()), "panic3");`
4242

4343
error: only a `panic!` in `if`-then statement
44-
--> $DIR/manual_assert.rs:53:5
44+
--> $DIR/manual_assert.rs:59:5
4545
|
4646
LL | / if b.is_empty() || a.is_empty() {
4747
LL | | panic!("panic4");
4848
LL | | }
4949
| |_____^ help: try: `assert!(!(b.is_empty() || a.is_empty()), "panic4");`
5050

5151
error: only a `panic!` in `if`-then statement
52-
--> $DIR/manual_assert.rs:56:5
52+
--> $DIR/manual_assert.rs:62:5
5353
|
5454
LL | / if a.is_empty() || !b.is_empty() {
5555
LL | | panic!("panic5");
5656
LL | | }
5757
| |_____^ help: try: `assert!(!(a.is_empty() || !b.is_empty()), "panic5");`
5858

59-
error: aborting due to 7 previous errors
59+
error: only a `panic!` in `if`-then statement
60+
--> $DIR/manual_assert.rs:65:5
61+
|
62+
LL | / if a.is_empty() {
63+
LL | | panic!("with expansion {}", one!())
64+
LL | | }
65+
| |_____^ help: try: `assert!(!a.is_empty(), "with expansion {}", one!());`
66+
67+
error: aborting due to 8 previous errors
6068

tests/ui/manual_assert.edition2021.fixed

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#![warn(clippy::manual_assert)]
77
#![allow(clippy::nonminimal_bool)]
88

9+
macro_rules! one {
10+
() => {
11+
1
12+
};
13+
}
14+
915
fn main() {
1016
let a = vec![1, 2, 3];
1117
let c = Some(2);
@@ -42,4 +48,5 @@ fn main() {
4248
assert!(!(a.is_empty() && !b.is_empty()), "panic3");
4349
assert!(!(b.is_empty() || a.is_empty()), "panic4");
4450
assert!(!(a.is_empty() || !b.is_empty()), "panic5");
51+
assert!(!a.is_empty(), "with expansion {}", one!());
4552
}
Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: only a `panic!` in `if`-then statement
2-
--> $DIR/manual_assert.rs:24:5
2+
--> $DIR/manual_assert.rs:30:5
33
|
44
LL | / if !a.is_empty() {
55
LL | | panic!("qaqaq{:?}", a);
@@ -9,52 +9,60 @@ LL | | }
99
= note: `-D clippy::manual-assert` implied by `-D warnings`
1010

1111
error: only a `panic!` in `if`-then statement
12-
--> $DIR/manual_assert.rs:27:5
12+
--> $DIR/manual_assert.rs:33:5
1313
|
1414
LL | / if !a.is_empty() {
1515
LL | | panic!("qwqwq");
1616
LL | | }
1717
| |_____^ help: try: `assert!(a.is_empty(), "qwqwq");`
1818

1919
error: only a `panic!` in `if`-then statement
20-
--> $DIR/manual_assert.rs:44:5
20+
--> $DIR/manual_assert.rs:50:5
2121
|
2222
LL | / if b.is_empty() {
2323
LL | | panic!("panic1");
2424
LL | | }
2525
| |_____^ help: try: `assert!(!b.is_empty(), "panic1");`
2626

2727
error: only a `panic!` in `if`-then statement
28-
--> $DIR/manual_assert.rs:47:5
28+
--> $DIR/manual_assert.rs:53:5
2929
|
3030
LL | / if b.is_empty() && a.is_empty() {
3131
LL | | panic!("panic2");
3232
LL | | }
3333
| |_____^ help: try: `assert!(!(b.is_empty() && a.is_empty()), "panic2");`
3434

3535
error: only a `panic!` in `if`-then statement
36-
--> $DIR/manual_assert.rs:50:5
36+
--> $DIR/manual_assert.rs:56:5
3737
|
3838
LL | / if a.is_empty() && !b.is_empty() {
3939
LL | | panic!("panic3");
4040
LL | | }
4141
| |_____^ help: try: `assert!(!(a.is_empty() && !b.is_empty()), "panic3");`
4242

4343
error: only a `panic!` in `if`-then statement
44-
--> $DIR/manual_assert.rs:53:5
44+
--> $DIR/manual_assert.rs:59:5
4545
|
4646
LL | / if b.is_empty() || a.is_empty() {
4747
LL | | panic!("panic4");
4848
LL | | }
4949
| |_____^ help: try: `assert!(!(b.is_empty() || a.is_empty()), "panic4");`
5050

5151
error: only a `panic!` in `if`-then statement
52-
--> $DIR/manual_assert.rs:56:5
52+
--> $DIR/manual_assert.rs:62:5
5353
|
5454
LL | / if a.is_empty() || !b.is_empty() {
5555
LL | | panic!("panic5");
5656
LL | | }
5757
| |_____^ help: try: `assert!(!(a.is_empty() || !b.is_empty()), "panic5");`
5858

59-
error: aborting due to 7 previous errors
59+
error: only a `panic!` in `if`-then statement
60+
--> $DIR/manual_assert.rs:65:5
61+
|
62+
LL | / if a.is_empty() {
63+
LL | | panic!("with expansion {}", one!())
64+
LL | | }
65+
| |_____^ help: try: `assert!(!a.is_empty(), "with expansion {}", one!());`
66+
67+
error: aborting due to 8 previous errors
6068

tests/ui/manual_assert.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@
66
#![warn(clippy::manual_assert)]
77
#![allow(clippy::nonminimal_bool)]
88

9+
macro_rules! one {
10+
() => {
11+
1
12+
};
13+
}
14+
915
fn main() {
1016
let a = vec![1, 2, 3];
1117
let c = Some(2);
@@ -56,4 +62,7 @@ fn main() {
5662
if a.is_empty() || !b.is_empty() {
5763
panic!("panic5");
5864
}
65+
if a.is_empty() {
66+
panic!("with expansion {}", one!())
67+
}
5968
}

0 commit comments

Comments
 (0)