Ignore format! with precision in USELESS_FORMAT

This commit is contained in:
mcarton 2018-10-02 23:55:25 +02:00
parent d18c7b2722
commit 7eebd5b20c
2 changed files with 12 additions and 4 deletions

View file

@ -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! {
@ -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;
}

View file

@ -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);
}