Skip to content

Commit 4450c21

Browse files
committed
Keep track of spans in format strings
1 parent 5b3e61d commit 4450c21

File tree

1 file changed

+148
-90
lines changed

1 file changed

+148
-90
lines changed

clippy_lints/src/write.rs

Lines changed: 148 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,18 @@ use std::borrow::Cow;
22
use std::ops::Range;
33

44
use clippy_utils::diagnostics::{span_lint, span_lint_and_sugg, span_lint_and_then};
5-
use clippy_utils::source::snippet_with_applicability;
6-
use if_chain::if_chain;
7-
use rustc_ast::ast::{Expr, ExprKind, ImplKind, Item, ItemKind, LitKind, MacCall, StrLit, StrStyle};
5+
use clippy_utils::source::{snippet_opt, snippet_with_applicability};
6+
use rustc_ast::ast::{Expr, ExprKind, ImplKind, Item, ItemKind, LitKind, MacCall, Path, StrLit, StrStyle};
87
use rustc_ast::token;
98
use rustc_ast::tokenstream::TokenStream;
109
use rustc_errors::Applicability;
1110
use rustc_lexer::unescape::{self, EscapeError};
1211
use rustc_lint::{EarlyContext, EarlyLintPass};
1312
use rustc_parse::parser;
1413
use rustc_session::{declare_tool_lint, impl_lint_pass};
15-
use rustc_span::symbol::kw;
16-
use rustc_span::{sym, BytePos, Span};
14+
use rustc_span::symbol::{kw, Symbol};
15+
use rustc_span::{sym, BytePos, Span, DUMMY_SP};
16+
use smallvec::SmallVec;
1717

1818
declare_clippy_lint! {
1919
/// **What it does:** This lint warns when you use `println!("")` to
@@ -354,7 +354,117 @@ fn newline_span(fmtstr: &StrLit) -> Span {
354354
sp.with_lo(newline_sp_hi - newline_sp_len).with_hi(newline_sp_hi)
355355
}
356356

357+
/// Stores a list of replacement spans for each argument, but only if all the replacements used an
358+
/// empty format string.
359+
#[derive(Default)]
360+
struct SimpleFormatArgs {
361+
unnamed: Vec<SmallVec<[Span; 1]>>,
362+
named: Vec<(Symbol, SmallVec<[Span; 1]>)>,
363+
}
364+
impl SimpleFormatArgs {
365+
fn get_unnamed(&self) -> impl Iterator<Item = &[Span]> {
366+
self.unnamed.iter().map(|x| match x.as_slice() {
367+
// Ignore the dummy span added from out of order format arguments.
368+
[DUMMY_SP] => &[],
369+
x => x,
370+
})
371+
}
372+
373+
fn get_named(&self, n: &Path) -> &[Span] {
374+
self.named.iter().find(|x| *n == x.0).map_or(&[], |x| x.1.as_slice())
375+
}
376+
377+
fn push(&mut self, arg: rustc_parse_format::Argument<'_>, span: Span) {
378+
use rustc_parse_format::{
379+
AlignUnknown, ArgumentImplicitlyIs, ArgumentIs, ArgumentNamed, CountImplied, FormatSpec,
380+
};
381+
382+
const SIMPLE: FormatSpec<'_> = FormatSpec {
383+
fill: None,
384+
align: AlignUnknown,
385+
flags: 0,
386+
precision: CountImplied,
387+
precision_span: None,
388+
width: CountImplied,
389+
width_span: None,
390+
ty: "",
391+
ty_span: None,
392+
};
393+
394+
match arg.position {
395+
ArgumentIs(n) | ArgumentImplicitlyIs(n) => {
396+
if self.unnamed.len() <= n {
397+
// Use a dummy span to mark all unseen arguments.
398+
self.unnamed.resize_with(n, || SmallVec::from([DUMMY_SP]));
399+
if arg.format == SIMPLE {
400+
self.unnamed.push(SmallVec::from([span]));
401+
} else {
402+
self.unnamed.push(SmallVec::new());
403+
}
404+
} else {
405+
let args = &mut self.unnamed[n];
406+
match (args.as_mut_slice(), arg.format == SIMPLE) {
407+
// A non-empty format string has been seen already.
408+
([], _) => (),
409+
// Replace the dummy span, if it exists.
410+
([dummy @ DUMMY_SP], true) => *dummy = span,
411+
([_, ..], true) => args.push(span),
412+
([_, ..], false) => *args = SmallVec::new(),
413+
}
414+
}
415+
},
416+
ArgumentNamed(n) => {
417+
if let Some(x) = self.named.iter_mut().find(|x| x.0 == n) {
418+
match x.1.as_slice() {
419+
// A non-empty format string has been seen already.
420+
[] => (),
421+
[_, ..] if arg.format == SIMPLE => x.1.push(span),
422+
[_, ..] => x.1 = SmallVec::new(),
423+
}
424+
} else if arg.format == SIMPLE {
425+
self.named.push((n, SmallVec::from([span])));
426+
} else {
427+
self.named.push((n, SmallVec::new()));
428+
}
429+
},
430+
};
431+
}
432+
}
433+
357434
impl Write {
435+
/// Parses a format string into a collection of spans for each argument. This only keeps track
436+
/// of empty format arguments. Will also lint usages of debug format strings outside of debug
437+
/// impls.
438+
fn parse_fmt_string(&self, cx: &EarlyContext<'_>, str: &StrLit) -> Option<SimpleFormatArgs> {
439+
use rustc_parse_format::{ParseMode, Parser, Piece};
440+
441+
let str_sym = str.symbol.as_str();
442+
let style = match str.style {
443+
StrStyle::Cooked => None,
444+
StrStyle::Raw(n) => Some(n as usize),
445+
};
446+
447+
let mut parser = Parser::new(&str_sym, style, snippet_opt(cx, str.span), false, ParseMode::Format);
448+
let mut args = SimpleFormatArgs::default();
449+
450+
while let Some(arg) = parser.next() {
451+
let arg = match arg {
452+
Piece::String(_) => continue,
453+
Piece::NextArgument(arg) => arg,
454+
};
455+
let span = parser.arg_places.last().map_or(DUMMY_SP, |&x| str.span.from_inner(x));
456+
457+
if !self.in_debug_impl && arg.format.ty == "?" {
458+
// FIXME: modify rustc's fmt string parser to give us the current span
459+
span_lint(cx, USE_DEBUG, str.span, "use of `Debug`-based formatting");
460+
}
461+
462+
args.push(arg, span);
463+
}
464+
465+
parser.errors.is_empty().then(move || args)
466+
}
467+
358468
/// Checks the arguments of `print[ln]!` and `write[ln]!` calls. It will return a tuple of two
359469
/// `Option`s. The first `Option` of the tuple is the macro's format string. It includes
360470
/// the contents of the string, whether it's a raw string, and the span of the literal in the
@@ -376,57 +486,31 @@ impl Write {
376486
/// ```
377487
#[allow(clippy::too_many_lines)]
378488
fn check_tts<'a>(&self, cx: &EarlyContext<'a>, tts: TokenStream, is_write: bool) -> (Option<StrLit>, Option<Expr>) {
379-
use rustc_parse_format::{
380-
AlignUnknown, ArgumentImplicitlyIs, ArgumentIs, ArgumentNamed, CountImplied, FormatSpec, ParseMode, Parser,
381-
Piece,
382-
};
383-
384489
let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, false, None);
385-
let mut expr: Option<Expr> = None;
386-
if is_write {
387-
expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
388-
Ok(p) => Some(p.into_inner()),
389-
Err(_) => return (None, None),
390-
};
391-
// might be `writeln!(foo)`
392-
if parser.expect(&token::Comma).map_err(|mut err| err.cancel()).is_err() {
393-
return (None, expr);
490+
let expr = if is_write {
491+
match parser.parse_expr().map(|e| e.into_inner()).map_err(|mut e| e.cancel()) {
492+
// write!(e, ...)
493+
Ok(p) if parser.eat(&token::Comma) => Some(p),
494+
// write!(e) or error
495+
e => return (None, e.ok()),
394496
}
395-
}
497+
} else {
498+
None
499+
};
396500

397501
let fmtstr = match parser.parse_str_lit() {
398502
Ok(fmtstr) => fmtstr,
399503
Err(_) => return (None, expr),
400504
};
401-
let tmp = fmtstr.symbol.as_str();
402-
let mut args = vec![];
403-
let mut fmt_parser = Parser::new(&tmp, None, None, false, ParseMode::Format);
404-
while let Some(piece) = fmt_parser.next() {
405-
if !fmt_parser.errors.is_empty() {
406-
return (None, expr);
407-
}
408-
if let Piece::NextArgument(arg) = piece {
409-
if !self.in_debug_impl && arg.format.ty == "?" {
410-
// FIXME: modify rustc's fmt string parser to give us the current span
411-
span_lint(cx, USE_DEBUG, parser.prev_token.span, "use of `Debug`-based formatting");
412-
}
413-
args.push(arg);
414-
}
415-
}
505+
506+
let args = match self.parse_fmt_string(cx, &fmtstr) {
507+
Some(args) => args,
508+
None => return (Some(fmtstr), expr),
509+
};
510+
416511
let lint = if is_write { WRITE_LITERAL } else { PRINT_LITERAL };
417-
let mut idx = 0;
512+
let mut unnamed_args = args.get_unnamed();
418513
loop {
419-
const SIMPLE: FormatSpec<'_> = FormatSpec {
420-
fill: None,
421-
align: AlignUnknown,
422-
flags: 0,
423-
precision: CountImplied,
424-
precision_span: None,
425-
width: CountImplied,
426-
width_span: None,
427-
ty: "",
428-
ty_span: None,
429-
};
430514
if !parser.eat(&token::Comma) {
431515
return (Some(fmtstr), expr);
432516
}
@@ -435,52 +519,26 @@ impl Write {
435519
} else {
436520
return (Some(fmtstr), None);
437521
};
438-
match &token_expr.kind {
522+
let (fmt_spans, span) = match &token_expr.kind {
439523
ExprKind::Lit(lit) if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) => {
440-
let mut all_simple = true;
441-
let mut seen = false;
442-
for arg in &args {
443-
match arg.position {
444-
ArgumentImplicitlyIs(n) | ArgumentIs(n) => {
445-
if n == idx {
446-
all_simple &= arg.format == SIMPLE;
447-
seen = true;
448-
}
449-
},
450-
ArgumentNamed(_) => {},
451-
}
452-
}
453-
if all_simple && seen {
454-
span_lint(cx, lint, token_expr.span, "literal with an empty format string");
455-
}
456-
idx += 1;
524+
(unnamed_args.next().unwrap_or(&[]), token_expr.span)
457525
},
458-
ExprKind::Assign(lhs, rhs, _) => {
459-
if_chain! {
460-
if let ExprKind::Lit(ref lit) = rhs.kind;
461-
if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..));
462-
if let ExprKind::Path(_, p) = &lhs.kind;
463-
then {
464-
let mut all_simple = true;
465-
let mut seen = false;
466-
for arg in &args {
467-
match arg.position {
468-
ArgumentImplicitlyIs(_) | ArgumentIs(_) => {},
469-
ArgumentNamed(name) => {
470-
if *p == name {
471-
seen = true;
472-
all_simple &= arg.format == SIMPLE;
473-
}
474-
},
475-
}
476-
}
477-
if all_simple && seen {
478-
span_lint(cx, lint, rhs.span, "literal with an empty format string");
479-
}
480-
}
481-
}
526+
ExprKind::Assign(lhs, rhs, _) => match (&lhs.kind, &rhs.kind) {
527+
(ExprKind::Path(_, p), ExprKind::Lit(lit))
528+
if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) =>
529+
{
530+
(args.get_named(p), rhs.span)
531+
},
532+
_ => continue,
482533
},
483-
_ => idx += 1,
534+
_ => {
535+
unnamed_args.next();
536+
continue;
537+
},
538+
};
539+
540+
if !fmt_spans.is_empty() {
541+
span_lint(cx, lint, span, "literal with an empty format string");
484542
}
485543
}
486544
}

0 commit comments

Comments
 (0)