Skip to content

Commit 20dbb27

Browse files
committed
Fix useless_format false positive
1 parent 3fc8dae commit 20dbb27

File tree

3 files changed

+7
-1
lines changed

3 files changed

+7
-1
lines changed

clippy_lints/src/format.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ use rustc_lint::{LateContext, LateLintPass, LintContext};
1212
use rustc_session::{declare_lint_pass, declare_tool_lint};
1313
use rustc_span::source_map::Span;
1414
use rustc_span::sym;
15+
use rustc_span::symbol::kw;
1516

1617
declare_clippy_lint! {
1718
/// **What it does:** Checks for the use of `format!("string literal with no
@@ -157,7 +158,8 @@ fn on_new_v1_fmt<'tcx>(cx: &LateContext<'tcx>, expr: &'tcx Expr<'_>) -> Option<S
157158
if let ExprKind::Array(pieces) = arr.kind;
158159
if pieces.len() == 1;
159160
if let ExprKind::Lit(ref lit) = pieces[0].kind;
160-
if let LitKind::Str(..) = lit.node;
161+
if let LitKind::Str(symbol, _) = lit.node;
162+
if symbol == kw::Empty;
161163
// Argument 2 in `new_v1_formatted()`
162164
if let ExprKind::AddrOf(BorrowKind::Ref, _, arg1) = args[1].kind;
163165
if let ExprKind::Match(matchee, arms, MatchSource::Normal) = arg1.kind;

tests/ui/format.fixed

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,4 +69,6 @@ fn main() {
6969
// Wrap it with braces
7070
let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
7171
let _s: String = (&*v.join("\n")).to_string();
72+
73+
format!("prepend {:+}", "s");
7274
}

tests/ui/format.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,6 @@ fn main() {
7171
// Wrap it with braces
7272
let v: Vec<String> = vec!["foo".to_string(), "bar".to_string()];
7373
let _s: String = format!("{}", &*v.join("\n"));
74+
75+
format!("prepend {:+}", "s");
7476
}

0 commit comments

Comments
 (0)