2019-11-03 04:41:22 +00:00
|
|
|
use std::borrow::Cow;
|
|
|
|
use std::ops::Range;
|
|
|
|
|
2019-05-23 18:11:38 +00:00
|
|
|
use crate::utils::{snippet_with_applicability, span_lint, span_lint_and_sugg, span_lint_and_then};
|
2021-01-30 17:06:34 +00:00
|
|
|
use if_chain::if_chain;
|
2021-01-29 07:31:08 +00:00
|
|
|
use rustc_ast::ast::{
|
|
|
|
Expr, ExprKind, ImplKind, Item, ItemKind, LitKind, MacCall, StrLit, StrStyle,
|
|
|
|
};
|
2020-03-01 03:23:33 +00:00
|
|
|
use rustc_ast::token;
|
|
|
|
use rustc_ast::tokenstream::TokenStream;
|
2018-12-29 15:04:45 +00:00
|
|
|
use rustc_errors::Applicability;
|
2019-11-03 04:41:22 +00:00
|
|
|
use rustc_lexer::unescape::{self, EscapeError};
|
2020-01-12 06:08:41 +00:00
|
|
|
use rustc_lint::{EarlyContext, EarlyLintPass};
|
2019-11-11 04:22:50 +00:00
|
|
|
use rustc_parse::parser;
|
2020-01-14 15:28:08 +00:00
|
|
|
use rustc_session::{declare_tool_lint, impl_lint_pass};
|
2021-01-15 09:56:44 +00:00
|
|
|
use rustc_span::symbol::kw;
|
|
|
|
use rustc_span::{sym, BytePos, Span};
|
2018-04-05 03:46:39 +00:00
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** This lint warns when you use `println!("")` to
|
|
|
|
/// print a newline.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** You should use `println!()`, which is simpler.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2020-07-14 12:59:59 +00:00
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// println!("");
|
2020-07-14 12:59:59 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// println!();
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2018-04-05 03:46:39 +00:00
|
|
|
pub PRINTLN_EMPTY_STRING,
|
|
|
|
style,
|
|
|
|
"using `println!(\"\")` with an empty string"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** This lint warns when you use `print!()` with a format
|
2020-07-14 12:59:59 +00:00
|
|
|
/// string that ends in a newline.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
|
|
|
/// **Why is this bad?** You should use `println!()` instead, which appends the
|
|
|
|
/// newline.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-09 07:51:23 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # let name = "World";
|
2019-03-05 16:50:33 +00:00
|
|
|
/// print!("Hello {}!\n", name);
|
|
|
|
/// ```
|
|
|
|
/// use println!() instead
|
2019-03-09 07:51:23 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # let name = "World";
|
2019-03-05 16:50:33 +00:00
|
|
|
/// println!("Hello {}!", name);
|
|
|
|
/// ```
|
2018-04-05 03:46:39 +00:00
|
|
|
pub PRINT_WITH_NEWLINE,
|
|
|
|
style,
|
2018-08-07 20:01:10 +00:00
|
|
|
"using `print!()` with a format string that ends in a single newline"
|
2018-04-05 03:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for printing on *stdout*. The purpose of this lint
|
|
|
|
/// is to catch debugging remnants.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** People often print on *stdout* while debugging an
|
|
|
|
/// application and might forget to remove those prints afterward.
|
|
|
|
///
|
|
|
|
/// **Known problems:** Only catches `print!` and `println!` calls.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// println!("Hello world!");
|
|
|
|
/// ```
|
2018-04-05 03:46:39 +00:00
|
|
|
pub PRINT_STDOUT,
|
|
|
|
restriction,
|
|
|
|
"printing on stdout"
|
|
|
|
}
|
|
|
|
|
2020-12-20 16:19:49 +00:00
|
|
|
declare_clippy_lint! {
|
|
|
|
/// **What it does:** Checks for printing on *stderr*. The purpose of this lint
|
|
|
|
/// is to catch debugging remnants.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** People often print on *stderr* while debugging an
|
|
|
|
/// application and might forget to remove those prints afterward.
|
|
|
|
///
|
|
|
|
/// **Known problems:** Only catches `eprint!` and `eprintln!` calls.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// eprintln!("Hello world!");
|
|
|
|
/// ```
|
|
|
|
pub PRINT_STDERR,
|
|
|
|
restriction,
|
|
|
|
"printing on stderr"
|
|
|
|
}
|
|
|
|
|
2018-04-05 03:46:39 +00:00
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** Checks for use of `Debug` formatting. The purpose of this
|
|
|
|
/// lint is to catch debugging remnants.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** The purpose of the `Debug` trait is to facilitate
|
2019-08-11 10:56:30 +00:00
|
|
|
/// debugging Rust code. It should not be used in user-facing output.
|
2019-03-05 16:50:33 +00:00
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
2019-08-03 19:24:50 +00:00
|
|
|
/// # let foo = "bar";
|
2019-03-05 16:50:33 +00:00
|
|
|
/// println!("{:?}", foo);
|
|
|
|
/// ```
|
2018-04-05 03:46:39 +00:00
|
|
|
pub USE_DEBUG,
|
|
|
|
restriction,
|
|
|
|
"use of `Debug`-based formatting"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** This lint warns about the use of literals as `print!`/`println!` args.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Using literals as `println!` args is inefficient
|
|
|
|
/// (c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary
|
|
|
|
/// (i.e., just put the literal in the format string)
|
|
|
|
///
|
|
|
|
/// **Known problems:** Will also warn with macro calls as arguments that expand to literals
|
|
|
|
/// -- e.g., `println!("{}", env!("FOO"))`.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
|
|
|
/// ```rust
|
|
|
|
/// println!("{}", "foo");
|
|
|
|
/// ```
|
|
|
|
/// use the literal without formatting:
|
|
|
|
/// ```rust
|
|
|
|
/// println!("foo");
|
|
|
|
/// ```
|
2018-04-05 03:46:39 +00:00
|
|
|
pub PRINT_LITERAL,
|
|
|
|
style,
|
|
|
|
"printing a literal with a format string"
|
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** This lint warns when you use `writeln!(buf, "")` to
|
|
|
|
/// print a newline.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** You should use `writeln!(buf)`, which is simpler.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-09 07:51:23 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # use std::fmt::Write;
|
|
|
|
/// # let mut buf = String::new();
|
2020-07-14 12:59:59 +00:00
|
|
|
///
|
|
|
|
/// // Bad
|
2019-03-05 22:23:50 +00:00
|
|
|
/// writeln!(buf, "");
|
2020-07-14 12:59:59 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// writeln!(buf);
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2018-04-05 03:46:39 +00:00
|
|
|
pub WRITELN_EMPTY_STRING,
|
|
|
|
style,
|
2019-03-05 22:23:50 +00:00
|
|
|
"using `writeln!(buf, \"\")` with an empty string"
|
2018-04-05 03:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** This lint warns when you use `write!()` with a format
|
|
|
|
/// string that
|
|
|
|
/// ends in a newline.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** You should use `writeln!()` instead, which appends the
|
|
|
|
/// newline.
|
|
|
|
///
|
|
|
|
/// **Known problems:** None.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-09 07:51:23 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # use std::fmt::Write;
|
|
|
|
/// # let mut buf = String::new();
|
|
|
|
/// # let name = "World";
|
2020-07-14 12:59:59 +00:00
|
|
|
///
|
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// write!(buf, "Hello {}!\n", name);
|
2020-07-14 12:59:59 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// writeln!(buf, "Hello {}!", name);
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2018-04-05 03:46:39 +00:00
|
|
|
pub WRITE_WITH_NEWLINE,
|
|
|
|
style,
|
2018-08-07 20:01:10 +00:00
|
|
|
"using `write!()` with a format string that ends in a single newline"
|
2018-04-05 03:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
declare_clippy_lint! {
|
2019-03-05 16:50:33 +00:00
|
|
|
/// **What it does:** This lint warns about the use of literals as `write!`/`writeln!` args.
|
|
|
|
///
|
|
|
|
/// **Why is this bad?** Using literals as `writeln!` args is inefficient
|
|
|
|
/// (c.f., https://github.com/matthiaskrgr/rust-str-bench) and unnecessary
|
|
|
|
/// (i.e., just put the literal in the format string)
|
|
|
|
///
|
|
|
|
/// **Known problems:** Will also warn with macro calls as arguments that expand to literals
|
|
|
|
/// -- e.g., `writeln!(buf, "{}", env!("FOO"))`.
|
|
|
|
///
|
|
|
|
/// **Example:**
|
2019-03-09 07:51:23 +00:00
|
|
|
/// ```rust
|
|
|
|
/// # use std::fmt::Write;
|
|
|
|
/// # let mut buf = String::new();
|
2020-07-14 12:59:59 +00:00
|
|
|
///
|
|
|
|
/// // Bad
|
2019-03-05 16:50:33 +00:00
|
|
|
/// writeln!(buf, "{}", "foo");
|
2020-07-14 12:59:59 +00:00
|
|
|
///
|
|
|
|
/// // Good
|
|
|
|
/// writeln!(buf, "foo");
|
2019-03-05 16:50:33 +00:00
|
|
|
/// ```
|
2018-04-05 03:46:39 +00:00
|
|
|
pub WRITE_LITERAL,
|
|
|
|
style,
|
|
|
|
"writing a literal with a format string"
|
|
|
|
}
|
|
|
|
|
2020-01-14 15:28:08 +00:00
|
|
|
#[derive(Default)]
|
|
|
|
pub struct Write {
|
|
|
|
in_debug_impl: bool,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl_lint_pass!(Write => [
|
2019-04-08 20:43:55 +00:00
|
|
|
PRINT_WITH_NEWLINE,
|
|
|
|
PRINTLN_EMPTY_STRING,
|
|
|
|
PRINT_STDOUT,
|
2020-12-20 16:19:49 +00:00
|
|
|
PRINT_STDERR,
|
2019-04-08 20:43:55 +00:00
|
|
|
USE_DEBUG,
|
|
|
|
PRINT_LITERAL,
|
|
|
|
WRITE_WITH_NEWLINE,
|
|
|
|
WRITELN_EMPTY_STRING,
|
|
|
|
WRITE_LITERAL
|
|
|
|
]);
|
2018-04-05 03:46:39 +00:00
|
|
|
|
2019-04-08 20:43:55 +00:00
|
|
|
impl EarlyLintPass for Write {
|
2020-01-14 15:28:08 +00:00
|
|
|
fn check_item(&mut self, _: &EarlyContext<'_>, item: &Item) {
|
2021-02-03 04:43:30 +00:00
|
|
|
if let ItemKind::Impl(box ImplKind {
|
|
|
|
of_trait: Some(trait_ref),
|
|
|
|
..
|
|
|
|
}) = &item.kind
|
|
|
|
{
|
2020-01-14 15:28:08 +00:00
|
|
|
let trait_name = trait_ref
|
|
|
|
.path
|
|
|
|
.segments
|
|
|
|
.iter()
|
|
|
|
.last()
|
|
|
|
.expect("path has at least one segment")
|
|
|
|
.ident
|
|
|
|
.name;
|
2020-11-05 13:29:48 +00:00
|
|
|
if trait_name == sym::Debug {
|
2020-01-14 15:28:08 +00:00
|
|
|
self.in_debug_impl = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn check_item_post(&mut self, _: &EarlyContext<'_>, _: &Item) {
|
|
|
|
self.in_debug_impl = false;
|
|
|
|
}
|
|
|
|
|
2020-03-15 17:23:43 +00:00
|
|
|
fn check_mac(&mut self, cx: &EarlyContext<'_>, mac: &MacCall) {
|
2020-10-09 10:45:29 +00:00
|
|
|
fn is_build_script(cx: &EarlyContext<'_>) -> bool {
|
|
|
|
// Cargo sets the crate name for build scripts to `build_script_build`
|
|
|
|
cx.sess
|
|
|
|
.opts
|
|
|
|
.crate_name
|
|
|
|
.as_ref()
|
|
|
|
.map_or(false, |crate_name| crate_name == "build_script_build")
|
|
|
|
}
|
|
|
|
|
2020-12-20 16:19:49 +00:00
|
|
|
if mac.path == sym!(print) {
|
2020-10-09 10:45:29 +00:00
|
|
|
if !is_build_script(cx) {
|
|
|
|
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `print!`");
|
|
|
|
}
|
2020-12-20 16:19:49 +00:00
|
|
|
self.lint_print_with_newline(cx, mac);
|
|
|
|
} else if mac.path == sym!(println) {
|
|
|
|
if !is_build_script(cx) {
|
|
|
|
span_lint(cx, PRINT_STDOUT, mac.span(), "use of `println!`");
|
2018-04-05 03:46:39 +00:00
|
|
|
}
|
2020-12-20 16:19:49 +00:00
|
|
|
self.lint_println_empty_string(cx, mac);
|
|
|
|
} else if mac.path == sym!(eprint) {
|
|
|
|
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprint!`");
|
|
|
|
self.lint_print_with_newline(cx, mac);
|
|
|
|
} else if mac.path == sym!(eprintln) {
|
|
|
|
span_lint(cx, PRINT_STDERR, mac.span(), "use of `eprintln!`");
|
|
|
|
self.lint_println_empty_string(cx, mac);
|
2019-08-16 02:30:38 +00:00
|
|
|
} else if mac.path == sym!(write) {
|
2020-08-28 14:10:16 +00:00
|
|
|
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), true) {
|
2019-11-18 15:38:36 +00:00
|
|
|
if check_newlines(&fmt_str) {
|
2019-05-23 18:11:38 +00:00
|
|
|
span_lint_and_then(
|
2018-09-06 10:33:00 +00:00
|
|
|
cx,
|
|
|
|
WRITE_WITH_NEWLINE,
|
2019-12-03 16:54:32 +00:00
|
|
|
mac.span(),
|
2019-05-23 18:11:38 +00:00
|
|
|
"using `write!()` with a format string that ends in a single newline",
|
|
|
|
|err| {
|
|
|
|
err.multipart_suggestion(
|
|
|
|
"use `writeln!()` instead",
|
|
|
|
vec![
|
2019-08-16 02:30:38 +00:00
|
|
|
(mac.path.span, String::from("writeln")),
|
2019-11-18 15:38:36 +00:00
|
|
|
(newline_span(&fmt_str), String::new()),
|
2019-05-23 18:11:38 +00:00
|
|
|
],
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
)
|
2018-04-05 03:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
2019-08-16 02:30:38 +00:00
|
|
|
} else if mac.path == sym!(writeln) {
|
2020-08-28 14:10:16 +00:00
|
|
|
if let (Some(fmt_str), expr) = self.check_tts(cx, mac.args.inner_tokens(), true) {
|
2021-01-15 09:56:44 +00:00
|
|
|
if fmt_str.symbol == kw::Empty {
|
2018-11-27 14:13:57 +00:00
|
|
|
let mut applicability = Applicability::MachineApplicable;
|
2020-08-28 14:10:16 +00:00
|
|
|
// FIXME: remove this `#[allow(...)]` once the issue #5822 gets fixed
|
|
|
|
#[allow(clippy::option_if_let_else)]
|
|
|
|
let suggestion = if let Some(e) = expr {
|
|
|
|
snippet_with_applicability(cx, e.span, "v", &mut applicability)
|
|
|
|
} else {
|
|
|
|
applicability = Applicability::HasPlaceholders;
|
|
|
|
Cow::Borrowed("v")
|
|
|
|
};
|
2018-08-20 12:03:13 +00:00
|
|
|
|
2018-07-22 22:19:07 +00:00
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
WRITELN_EMPTY_STRING,
|
2019-12-03 16:54:32 +00:00
|
|
|
mac.span(),
|
2018-08-20 13:50:15 +00:00
|
|
|
format!("using `writeln!({}, \"\")`", suggestion).as_str(),
|
2018-07-22 22:19:07 +00:00
|
|
|
"replace it with",
|
2018-08-20 13:33:43 +00:00
|
|
|
format!("writeln!({})", suggestion),
|
2018-11-27 14:13:57 +00:00
|
|
|
applicability,
|
2018-07-22 22:19:07 +00:00
|
|
|
);
|
2018-04-05 03:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:38:36 +00:00
|
|
|
/// Given a format string that ends in a newline and its span, calculates the span of the
|
2020-09-24 12:49:22 +00:00
|
|
|
/// newline, or the format string itself if the format string consists solely of a newline.
|
2019-11-18 15:38:36 +00:00
|
|
|
fn newline_span(fmtstr: &StrLit) -> Span {
|
|
|
|
let sp = fmtstr.span;
|
|
|
|
let contents = &fmtstr.symbol.as_str();
|
2019-05-23 18:11:38 +00:00
|
|
|
|
2020-09-24 12:49:22 +00:00
|
|
|
if *contents == r"\n" {
|
|
|
|
return sp;
|
|
|
|
}
|
|
|
|
|
2019-11-18 15:38:36 +00:00
|
|
|
let newline_sp_hi = sp.hi()
|
|
|
|
- match fmtstr.style {
|
|
|
|
StrStyle::Cooked => BytePos(1),
|
|
|
|
StrStyle::Raw(hashes) => BytePos((1 + hashes).into()),
|
2019-05-23 18:11:38 +00:00
|
|
|
};
|
|
|
|
|
2019-11-18 15:38:36 +00:00
|
|
|
let newline_sp_len = if contents.ends_with('\n') {
|
|
|
|
BytePos(1)
|
|
|
|
} else if contents.ends_with(r"\n") {
|
|
|
|
BytePos(2)
|
|
|
|
} else {
|
|
|
|
panic!("expected format string to contain a newline");
|
|
|
|
};
|
|
|
|
|
|
|
|
sp.with_lo(newline_sp_hi - newline_sp_len).with_hi(newline_sp_hi)
|
2019-05-23 18:11:38 +00:00
|
|
|
}
|
|
|
|
|
2020-01-14 15:28:08 +00:00
|
|
|
impl Write {
|
|
|
|
/// Checks the arguments of `print[ln]!` and `write[ln]!` calls. It will return a tuple of two
|
|
|
|
/// `Option`s. The first `Option` of the tuple is the macro's format string. It includes
|
|
|
|
/// the contents of the string, whether it's a raw string, and the span of the literal in the
|
|
|
|
/// source. The second `Option` in the tuple is, in the `write[ln]!` case, the expression the
|
|
|
|
/// `format_str` should be written to.
|
|
|
|
///
|
|
|
|
/// Example:
|
|
|
|
///
|
|
|
|
/// Calling this function on
|
|
|
|
/// ```rust
|
|
|
|
/// # use std::fmt::Write;
|
|
|
|
/// # let mut buf = String::new();
|
|
|
|
/// # let something = "something";
|
|
|
|
/// writeln!(buf, "string to write: {}", something);
|
|
|
|
/// ```
|
|
|
|
/// will return
|
|
|
|
/// ```rust,ignore
|
|
|
|
/// (Some("string to write: {}"), Some(buf))
|
|
|
|
/// ```
|
|
|
|
#[allow(clippy::too_many_lines)]
|
2021-01-29 07:31:08 +00:00
|
|
|
fn check_tts<'a>(
|
|
|
|
&self,
|
|
|
|
cx: &EarlyContext<'a>,
|
|
|
|
tts: TokenStream,
|
|
|
|
is_write: bool,
|
|
|
|
) -> (Option<StrLit>, Option<Expr>) {
|
2020-06-02 17:19:49 +00:00
|
|
|
use rustc_parse_format::{
|
2021-01-29 07:31:08 +00:00
|
|
|
AlignUnknown, ArgumentImplicitlyIs, ArgumentIs, ArgumentNamed, CountImplied,
|
|
|
|
FormatSpec, ParseMode, Parser, Piece,
|
2018-08-20 12:03:13 +00:00
|
|
|
};
|
2018-08-16 16:20:06 +00:00
|
|
|
|
2020-03-17 09:18:22 +00:00
|
|
|
let mut parser = parser::Parser::new(&cx.sess.parse_sess, tts, false, None);
|
2020-01-14 15:28:08 +00:00
|
|
|
let mut expr: Option<Expr> = None;
|
|
|
|
if is_write {
|
|
|
|
expr = match parser.parse_expr().map_err(|mut err| err.cancel()) {
|
|
|
|
Ok(p) => Some(p.into_inner()),
|
|
|
|
Err(_) => return (None, None),
|
|
|
|
};
|
|
|
|
// might be `writeln!(foo)`
|
|
|
|
if parser.expect(&token::Comma).map_err(|mut err| err.cancel()).is_err() {
|
|
|
|
return (None, expr);
|
2018-04-05 03:46:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-14 15:28:08 +00:00
|
|
|
|
|
|
|
let fmtstr = match parser.parse_str_lit() {
|
|
|
|
Ok(fmtstr) => fmtstr,
|
|
|
|
Err(_) => return (None, expr),
|
2018-07-22 22:19:07 +00:00
|
|
|
};
|
2020-01-14 15:28:08 +00:00
|
|
|
let tmp = fmtstr.symbol.as_str();
|
|
|
|
let mut args = vec![];
|
2020-05-06 14:03:53 +00:00
|
|
|
let mut fmt_parser = Parser::new(&tmp, None, None, false, ParseMode::Format);
|
2020-01-14 15:28:08 +00:00
|
|
|
while let Some(piece) = fmt_parser.next() {
|
|
|
|
if !fmt_parser.errors.is_empty() {
|
|
|
|
return (None, expr);
|
|
|
|
}
|
|
|
|
if let Piece::NextArgument(arg) = piece {
|
|
|
|
if !self.in_debug_impl && arg.format.ty == "?" {
|
|
|
|
// FIXME: modify rustc's fmt string parser to give us the current span
|
2021-01-29 07:31:08 +00:00
|
|
|
span_lint(
|
|
|
|
cx,
|
|
|
|
USE_DEBUG,
|
|
|
|
parser.prev_token.span,
|
|
|
|
"use of `Debug`-based formatting",
|
|
|
|
);
|
2020-01-14 15:28:08 +00:00
|
|
|
}
|
|
|
|
args.push(arg);
|
|
|
|
}
|
2018-10-08 00:08:20 +00:00
|
|
|
}
|
2020-01-14 15:28:08 +00:00
|
|
|
let lint = if is_write { WRITE_LITERAL } else { PRINT_LITERAL };
|
|
|
|
let mut idx = 0;
|
|
|
|
loop {
|
|
|
|
const SIMPLE: FormatSpec<'_> = FormatSpec {
|
|
|
|
fill: None,
|
|
|
|
align: AlignUnknown,
|
|
|
|
flags: 0,
|
|
|
|
precision: CountImplied,
|
|
|
|
precision_span: None,
|
|
|
|
width: CountImplied,
|
|
|
|
width_span: None,
|
|
|
|
ty: "",
|
|
|
|
ty_span: None,
|
|
|
|
};
|
|
|
|
if !parser.eat(&token::Comma) {
|
|
|
|
return (Some(fmtstr), expr);
|
|
|
|
}
|
|
|
|
let token_expr = if let Ok(expr) = parser.parse_expr().map_err(|mut err| err.cancel()) {
|
|
|
|
expr
|
|
|
|
} else {
|
|
|
|
return (Some(fmtstr), None);
|
|
|
|
};
|
|
|
|
match &token_expr.kind {
|
2021-01-29 07:31:08 +00:00
|
|
|
ExprKind::Lit(lit)
|
|
|
|
if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..)) =>
|
|
|
|
{
|
2020-01-14 15:28:08 +00:00
|
|
|
let mut all_simple = true;
|
|
|
|
let mut seen = false;
|
|
|
|
for arg in &args {
|
|
|
|
match arg.position {
|
|
|
|
ArgumentImplicitlyIs(n) | ArgumentIs(n) => {
|
|
|
|
if n == idx {
|
|
|
|
all_simple &= arg.format == SIMPLE;
|
|
|
|
seen = true;
|
|
|
|
}
|
2021-01-29 07:31:08 +00:00
|
|
|
}
|
|
|
|
ArgumentNamed(_) => {}
|
2020-01-14 15:28:08 +00:00
|
|
|
}
|
2018-07-22 22:19:07 +00:00
|
|
|
}
|
2020-01-14 15:28:08 +00:00
|
|
|
if all_simple && seen {
|
|
|
|
span_lint(cx, lint, token_expr.span, "literal with an empty format string");
|
|
|
|
}
|
|
|
|
idx += 1;
|
2021-01-29 07:31:08 +00:00
|
|
|
}
|
2020-01-14 15:28:08 +00:00
|
|
|
ExprKind::Assign(lhs, rhs, _) => {
|
2021-01-30 17:06:34 +00:00
|
|
|
if_chain! {
|
|
|
|
if let ExprKind::Lit(ref lit) = rhs.kind;
|
|
|
|
if !matches!(lit.kind, LitKind::Int(..) | LitKind::Float(..));
|
|
|
|
if let ExprKind::Path(_, p) = &lhs.kind;
|
|
|
|
then {
|
2020-01-14 15:28:08 +00:00
|
|
|
let mut all_simple = true;
|
|
|
|
let mut seen = false;
|
|
|
|
for arg in &args {
|
|
|
|
match arg.position {
|
|
|
|
ArgumentImplicitlyIs(_) | ArgumentIs(_) => {},
|
|
|
|
ArgumentNamed(name) => {
|
|
|
|
if *p == name {
|
|
|
|
seen = true;
|
|
|
|
all_simple &= arg.format == SIMPLE;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if all_simple && seen {
|
|
|
|
span_lint(cx, lint, rhs.span, "literal with an empty format string");
|
2018-07-25 21:31:17 +00:00
|
|
|
}
|
2018-07-22 22:19:07 +00:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 07:31:08 +00:00
|
|
|
}
|
2020-01-14 15:28:08 +00:00
|
|
|
_ => idx += 1,
|
|
|
|
}
|
2018-04-21 18:24:55 +00:00
|
|
|
}
|
|
|
|
}
|
2020-12-20 16:19:49 +00:00
|
|
|
|
|
|
|
fn lint_println_empty_string(&self, cx: &EarlyContext<'_>, mac: &MacCall) {
|
|
|
|
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
|
2021-01-15 09:56:44 +00:00
|
|
|
if fmt_str.symbol == kw::Empty {
|
2020-12-20 16:19:49 +00:00
|
|
|
let name = mac.path.segments[0].ident.name;
|
|
|
|
span_lint_and_sugg(
|
|
|
|
cx,
|
|
|
|
PRINTLN_EMPTY_STRING,
|
|
|
|
mac.span(),
|
|
|
|
&format!("using `{}!(\"\")`", name),
|
|
|
|
"replace it with",
|
|
|
|
format!("{}!()", name),
|
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn lint_print_with_newline(&self, cx: &EarlyContext<'_>, mac: &MacCall) {
|
|
|
|
if let (Some(fmt_str), _) = self.check_tts(cx, mac.args.inner_tokens(), false) {
|
|
|
|
if check_newlines(&fmt_str) {
|
|
|
|
let name = mac.path.segments[0].ident.name;
|
|
|
|
let suggested = format!("{}ln", name);
|
|
|
|
span_lint_and_then(
|
|
|
|
cx,
|
|
|
|
PRINT_WITH_NEWLINE,
|
|
|
|
mac.span(),
|
2021-01-29 07:31:08 +00:00
|
|
|
&format!(
|
|
|
|
"using `{}!()` with a format string that ends in a single newline",
|
|
|
|
name
|
|
|
|
),
|
2020-12-20 16:19:49 +00:00
|
|
|
|err| {
|
|
|
|
err.multipart_suggestion(
|
|
|
|
&format!("use `{}!` instead", suggested),
|
2021-01-29 07:31:08 +00:00
|
|
|
vec![
|
|
|
|
(mac.path.span, suggested),
|
|
|
|
(newline_span(&fmt_str), String::new()),
|
|
|
|
],
|
2020-12-20 16:19:49 +00:00
|
|
|
Applicability::MachineApplicable,
|
|
|
|
);
|
|
|
|
},
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-21 18:24:55 +00:00
|
|
|
}
|
2018-12-11 06:28:25 +00:00
|
|
|
|
2019-11-03 04:41:22 +00:00
|
|
|
/// Checks if the format string contains a single newline that terminates it.
|
2019-05-23 18:11:38 +00:00
|
|
|
///
|
|
|
|
/// Literal and escaped newlines are both checked (only literal for raw strings).
|
2019-11-18 15:38:36 +00:00
|
|
|
fn check_newlines(fmtstr: &StrLit) -> bool {
|
2019-11-03 04:41:22 +00:00
|
|
|
let mut has_internal_newline = false;
|
|
|
|
let mut last_was_cr = false;
|
|
|
|
let mut should_lint = false;
|
2019-05-23 18:11:38 +00:00
|
|
|
|
2019-11-18 15:38:36 +00:00
|
|
|
let contents = &fmtstr.symbol.as_str();
|
|
|
|
|
2019-11-03 04:41:22 +00:00
|
|
|
let mut cb = |r: Range<usize>, c: Result<char, EscapeError>| {
|
|
|
|
let c = c.unwrap();
|
2018-12-11 06:28:25 +00:00
|
|
|
|
2019-11-03 04:41:22 +00:00
|
|
|
if r.end == contents.len() && c == '\n' && !last_was_cr && !has_internal_newline {
|
|
|
|
should_lint = true;
|
|
|
|
} else {
|
|
|
|
last_was_cr = c == '\r';
|
|
|
|
if c == '\n' {
|
|
|
|
has_internal_newline = true;
|
2018-12-11 06:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
2019-11-03 04:41:22 +00:00
|
|
|
};
|
|
|
|
|
2019-11-18 15:38:36 +00:00
|
|
|
match fmtstr.style {
|
2020-05-13 08:03:49 +00:00
|
|
|
StrStyle::Cooked => unescape::unescape_literal(contents, unescape::Mode::Str, &mut cb),
|
|
|
|
StrStyle::Raw(_) => unescape::unescape_literal(contents, unescape::Mode::RawStr, &mut cb),
|
2018-12-11 06:28:25 +00:00
|
|
|
}
|
|
|
|
|
2019-11-03 04:41:22 +00:00
|
|
|
should_lint
|
2018-12-11 06:28:25 +00:00
|
|
|
}
|