From ef4401d4acf2eb1e125b9dd01d1baef195e2f53b Mon Sep 17 00:00:00 2001 From: mcarton Date: Sat, 20 Feb 2016 17:35:07 +0100 Subject: [PATCH] Lint about usage of `format!("string literal")` --- README.md | 3 ++- src/format.rs | 40 ++++++++++++++++++++++++++++++++++++ src/lib.rs | 2 ++ tests/compile-fail/format.rs | 11 ++++++++++ 4 files changed, 55 insertions(+), 1 deletion(-) create mode 100644 src/format.rs create mode 100755 tests/compile-fail/format.rs diff --git a/README.md b/README.md index 7fa0a15715f4..62d6695b24d2 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ A collection of lints to catch common mistakes and improve your Rust code. [Jump to usage instructions](#usage) ##Lints -There are 122 lints included in this crate: +There are 123 lints included in this crate: name | default | meaning ---------------------------------------------------------------------------------------------------------------|---------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ @@ -126,6 +126,7 @@ name [unused_lifetimes](https://github.com/Manishearth/rust-clippy/wiki#unused_lifetimes) | warn | unused lifetimes in function definitions [use_debug](https://github.com/Manishearth/rust-clippy/wiki#use_debug) | allow | use `Debug`-based formatting [used_underscore_binding](https://github.com/Manishearth/rust-clippy/wiki#used_underscore_binding) | warn | using a binding which is prefixed with an underscore +[useless_format](https://github.com/Manishearth/rust-clippy/wiki#useless_format) | warn | useless use of `format!` [useless_transmute](https://github.com/Manishearth/rust-clippy/wiki#useless_transmute) | warn | transmutes that have the same to and from types [useless_vec](https://github.com/Manishearth/rust-clippy/wiki#useless_vec) | warn | useless `vec!` [while_let_loop](https://github.com/Manishearth/rust-clippy/wiki#while_let_loop) | warn | `loop { if let { ... } else break }` can be written as a `while let` loop diff --git a/src/format.rs b/src/format.rs new file mode 100644 index 000000000000..ad72b38e1107 --- /dev/null +++ b/src/format.rs @@ -0,0 +1,40 @@ +use rustc::lint::*; +use rustc_front::hir::*; +use utils::{is_expn_of, span_lint}; + +/// **What it does:** This lints about use of `format!("string literal with no argument")`. +/// +/// **Why is this bad?** There is no point of doing that. If you want a `String` you can use +/// `to_owned` on the string literal. The even worst `&format!("foo")` is often encountered in the +/// wild. +/// +/// **Known problems:** None. +/// +/// **Example:** `format!("foo")` +declare_lint! { + pub USELESS_FORMAT, + Warn, + "useless use of `format!`" +} + +#[derive(Copy, Clone, Debug)] +pub struct FormatMacLint; + +impl LintPass for FormatMacLint { + fn get_lints(&self) -> LintArray { + lint_array![USELESS_FORMAT] + } +} + +impl LateLintPass for FormatMacLint { + fn check_expr(&mut self, cx: &LateContext, expr: &Expr) { + // `format!("foo")` expansion contains `match () { () => [], }` + if let ExprMatch(ref matchee, _, _) = expr.node { + if let ExprTup(ref tup) = matchee.node { + if tup.is_empty() && is_expn_of(cx, expr.span, "format").is_some() { + span_lint(cx, USELESS_FORMAT, expr.span, &"useless use of `format!`"); + } + } + } + } +} diff --git a/src/lib.rs b/src/lib.rs index 23accd2ebaac..b690a5cf45eb 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -163,6 +163,7 @@ pub fn plugin_registrar(reg: &mut Registry) { reg.register_late_lint_pass(box types::AbsurdExtremeComparisons); reg.register_late_lint_pass(box regex::RegexPass::default()); reg.register_late_lint_pass(box copies::CopyAndPaste); + reg.register_late_lint_pass(box format::FormatMacLint); reg.register_lint_group("clippy_pedantic", vec![ enum_glob_use::ENUM_GLOB_USE, @@ -209,6 +210,7 @@ pub fn plugin_registrar(reg: &mut Registry) { eq_op::EQ_OP, escape::BOXED_LOCAL, eta_reduction::REDUNDANT_CLOSURE, + format::USELESS_FORMAT, identity_op::IDENTITY_OP, items_after_statements::ITEMS_AFTER_STATEMENTS, len_zero::LEN_WITHOUT_IS_EMPTY, diff --git a/tests/compile-fail/format.rs b/tests/compile-fail/format.rs new file mode 100755 index 000000000000..6cdceefd063e --- /dev/null +++ b/tests/compile-fail/format.rs @@ -0,0 +1,11 @@ +#![feature(plugin)] +#![plugin(clippy)] +#![deny(useless_format)] + +fn main() { + format!("foo"); //~ERROR useless use of `format!` + format!("foo {}", 42); + + println!("foo"); + println!("foo {}", 42); +}