Skip to content

Commit 7eebd5b

Browse files
committed
Ignore format! with precision in USELESS_FORMAT
1 parent d18c7b2 commit 7eebd5b

File tree

2 files changed

+12
-4
lines changed

2 files changed

+12
-4
lines changed

clippy_lints/src/format.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
4747
return;
4848
}
4949
match expr.node {
50-
5150
// `format!("{}", foo)` expansion
5251
ExprKind::Call(ref fun, ref args) => {
5352
if_chain! {
@@ -162,9 +161,12 @@ fn check_unformatted(expr: &Expr) -> bool {
162161
if let ExprKind::Struct(_, ref fields, _) = exprs[0].node;
163162
if let Some(format_field) = fields.iter().find(|f| f.ident.name == "format");
164163
if let ExprKind::Struct(_, ref fields, _) = format_field.expr.node;
165-
if let Some(align_field) = fields.iter().find(|f| f.ident.name == "width");
166-
if let ExprKind::Path(ref qpath) = align_field.expr.node;
167-
if last_path_segment(qpath).ident.name == "Implied";
164+
if let Some(width_field) = fields.iter().find(|f| f.ident.name == "width");
165+
if let ExprKind::Path(ref width_qpath) = width_field.expr.node;
166+
if last_path_segment(width_qpath).ident.name == "Implied";
167+
if let Some(precision_field) = fields.iter().find(|f| f.ident.name == "precision");
168+
if let ExprKind::Path(ref precision_path) = precision_field.expr.node;
169+
if last_path_segment(precision_path).ident.name == "Implied";
168170
then {
169171
return true;
170172
}

tests/ui/format.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,10 @@ fn main() {
4646

4747
// A format! inside a macro should not trigger a warning
4848
foo!("should not warn");
49+
50+
// precision on string means slicing without panicking on size:
51+
format!("{:.1}", "foo"); // could be "foo"[..1]
52+
format!("{:.10}", "foo"); // could not be "foo"[..10]
53+
format!("{:.prec$}", "foo", prec = 1);
54+
format!("{:.prec$}", "foo", prec = 10);
4955
}

0 commit comments

Comments
 (0)