Lint about usage of format!("string literal")

This commit is contained in:
mcarton 2016-02-20 17:35:07 +01:00
parent 222086d62b
commit ef4401d4ac
4 changed files with 55 additions and 1 deletions

View file

@ -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

40
src/format.rs Normal file
View file

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

View file

@ -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,

11
tests/compile-fail/format.rs Executable file
View file

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