Skip to content

Commit 62d595b

Browse files
authored
Merge pull request rust-lang#2632 from phansch/fix_useless_format_false_positive
Fix useless_format false positive with macros
2 parents 3994880 + ff98e3f commit 62d595b

File tree

3 files changed

+19
-7
lines changed

3 files changed

+19
-7
lines changed

clippy_lints/src/format.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use rustc::lint::*;
33
use rustc::ty;
44
use syntax::ast::LitKind;
55
use utils::paths;
6-
use utils::{is_expn_of, match_def_path, match_type, opt_def_id, resolve_node, snippet, span_lint_and_then, walk_ptrs_ty};
6+
use utils::{in_macro, is_expn_of, match_def_path, match_type, opt_def_id, resolve_node, snippet, span_lint_and_then, walk_ptrs_ty};
77

88
/// **What it does:** Checks for the use of `format!("string literal with no
99
/// argument")` and `format!("{}", foo)` where `foo` is a string.
@@ -39,6 +39,9 @@ impl LintPass for Pass {
3939
impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
4040
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, expr: &'tcx Expr) {
4141
if let Some(span) = is_expn_of(expr.span, "format") {
42+
if in_macro(span) {
43+
return;
44+
}
4245
match expr.node {
4346
// `format!("{}", foo)` expansion
4447
ExprCall(ref fun, ref args) => {

tests/ui/format.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,12 @@
22
#![allow(print_literal)]
33
#![warn(useless_format)]
44

5+
struct Foo(pub String);
6+
7+
macro_rules! foo {
8+
($($t:tt)*) => (Foo(format!($($t)*)))
9+
}
10+
511
fn main() {
612
format!("foo");
713

@@ -31,4 +37,7 @@ fn main() {
3137
println!("foo {}", "foo");
3238
println!("{}", 42);
3339
println!("foo {}", 42);
40+
41+
// A format! inside a macro should not trigger a warning
42+
foo!("should not warn");
3443
}

tests/ui/format.stderr

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
error: useless use of `format!`
2-
--> $DIR/format.rs:6:5
3-
|
4-
6 | format!("foo");
5-
| ^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string()`
6-
|
7-
= note: `-D useless-format` implied by `-D warnings`
2+
--> $DIR/format.rs:12:5
3+
|
4+
12 | format!("foo");
5+
| ^^^^^^^^^^^^^^^ help: consider using .to_string(): `"foo".to_string()`
6+
|
7+
= note: `-D useless-format` implied by `-D warnings`
88

99
error: aborting due to previous error
1010

0 commit comments

Comments
 (0)