Improve suggestion for missing fmt str in println

Avoid using `concat!(fmt, "\n")` to improve the diagnostics being
emitted when the first `println!()` argument isn't a formatting string
literal.
This commit is contained in:
Esteban Küber
2018-07-14 20:50:30 -07:00
committed by Esteban Küber
parent bc14d71622
commit f53c145ef1
14 changed files with 122 additions and 102 deletions

View File

@@ -27,6 +27,7 @@ pub fn expand_syntax_ext(
None => return base::DummyResult::expr(sp),
};
let mut accumulator = String::new();
let mut missing_literal = vec![];
for e in es {
match e.node {
ast::ExprKind::Lit(ref lit) => match lit.node {
@@ -51,17 +52,15 @@ pub fn expand_syntax_ext(
}
},
_ => {
let mut err = cx.struct_span_err(e.span, "expected a literal");
let snippet = cx.codemap().span_to_snippet(e.span).unwrap();
err.span_suggestion(
e.span,
"you might be missing a string literal to format with",
format!("\"{{}}\", {}", snippet),
);
err.emit();
missing_literal.push(e.span);
}
}
}
if missing_literal.len() > 0 {
let mut err = cx.struct_span_err(missing_literal, "expected a literal");
err.note("only `&str` literals can be passed to `concat!()`");
err.emit();
}
let sp = sp.apply_mark(cx.current_expansion.mark);
base::MacEager::expr(cx.expr_str(sp, Symbol::intern(&accumulator)))
}