Skip to content

Commit

Permalink
Ignore format! with precision in USELESS_FORMAT
Browse files Browse the repository at this point in the history
  • Loading branch information
mcarton committed Oct 2, 2018
1 parent d18c7b2 commit 7eebd5b
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 4 deletions.
10 changes: 6 additions & 4 deletions clippy_lints/src/format.rs
Expand Up @@ -47,7 +47,6 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
return;
}
match expr.node {

// `format!("{}", foo)` expansion
ExprKind::Call(ref fun, ref args) => {
if_chain! {
Expand Down Expand Up @@ -162,9 +161,12 @@ fn check_unformatted(expr: &Expr) -> bool {
if let ExprKind::Struct(_, ref fields, _) = exprs[0].node;
if let Some(format_field) = fields.iter().find(|f| f.ident.name == "format");
if let ExprKind::Struct(_, ref fields, _) = format_field.expr.node;
if let Some(align_field) = fields.iter().find(|f| f.ident.name == "width");
if let ExprKind::Path(ref qpath) = align_field.expr.node;
if last_path_segment(qpath).ident.name == "Implied";
if let Some(width_field) = fields.iter().find(|f| f.ident.name == "width");
if let ExprKind::Path(ref width_qpath) = width_field.expr.node;
if last_path_segment(width_qpath).ident.name == "Implied";
if let Some(precision_field) = fields.iter().find(|f| f.ident.name == "precision");
if let ExprKind::Path(ref precision_path) = precision_field.expr.node;
if last_path_segment(precision_path).ident.name == "Implied";
then {
return true;
}
Expand Down
6 changes: 6 additions & 0 deletions tests/ui/format.rs
Expand Up @@ -46,4 +46,10 @@ fn main() {

// A format! inside a macro should not trigger a warning
foo!("should not warn");

// precision on string means slicing without panicking on size:
format!("{:.1}", "foo"); // could be "foo"[..1]
format!("{:.10}", "foo"); // could not be "foo"[..10]
format!("{:.prec$}", "foo", prec = 1);
format!("{:.prec$}", "foo", prec = 10);
}

0 comments on commit 7eebd5b

Please sign in to comment.