mirror of
https://github.com/rust-lang/rust-clippy
synced 2024-11-11 07:34:18 +00:00
pulled strings passes together, added more tests
This commit is contained in:
parent
f0182ca6c8
commit
e6e036ec20
3 changed files with 44 additions and 20 deletions
|
@ -56,7 +56,6 @@ pub fn plugin_registrar(reg: &mut Registry) {
|
|||
reg.register_lint_pass(box misc::ModuloOne as LintPassObject);
|
||||
reg.register_lint_pass(box unicode::Unicode as LintPassObject);
|
||||
reg.register_lint_pass(box strings::StringAdd as LintPassObject);
|
||||
reg.register_lint_pass(box strings::StringAddAssign as LintPassObject);
|
||||
reg.register_lint_pass(box returns::ReturnPass as LintPassObject);
|
||||
reg.register_lint_pass(box methods::MethodsPass as LintPassObject);
|
||||
|
||||
|
|
|
@ -28,7 +28,7 @@ pub struct StringAdd;
|
|||
|
||||
impl LintPass for StringAdd {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(STRING_ADD)
|
||||
lint_array!(STRING_ADD, STRING_ADD_ASSIGN)
|
||||
}
|
||||
|
||||
fn check_expr(&mut self, cx: &Context, e: &Expr) {
|
||||
|
@ -50,21 +50,7 @@ impl LintPass for StringAdd {
|
|||
"you add something to a string. \
|
||||
Consider using `String::push_str()` instead.")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Copy, Clone)]
|
||||
pub struct StringAddAssign;
|
||||
|
||||
impl LintPass for StringAddAssign {
|
||||
fn get_lints(&self) -> LintArray {
|
||||
lint_array!(STRING_ADD_ASSIGN)
|
||||
}
|
||||
|
||||
fn check_expr(&mut self, cx: &Context, e: &Expr) {
|
||||
if let &ExprAssign(ref target, ref src) = &e.node {
|
||||
} else if let &ExprAssign(ref target, ref src) = &e.node {
|
||||
if is_string(cx, target) && is_add(src, target) {
|
||||
span_lint(cx, STRING_ADD_ASSIGN, e.span,
|
||||
"you assign the result of adding something to this string. \
|
||||
|
|
|
@ -1,9 +1,37 @@
|
|||
#![feature(plugin)]
|
||||
#![plugin(clippy)]
|
||||
|
||||
#![deny(string_add_assign)]
|
||||
#![deny(string_add)]
|
||||
fn main() {
|
||||
#[deny(string_add)]
|
||||
#[allow(string_add_assign)]
|
||||
fn add_only() { // ignores assignment distinction
|
||||
let mut x = "".to_owned();
|
||||
|
||||
for _ in (1..3) {
|
||||
x = x + "."; //~ERROR you add something to a string.
|
||||
}
|
||||
|
||||
let y = "".to_owned();
|
||||
let z = y + "..."; //~ERROR you add something to a string.
|
||||
|
||||
assert_eq!(&x, &z);
|
||||
}
|
||||
|
||||
#[deny(string_add_assign)]
|
||||
fn add_assign_only() {
|
||||
let mut x = "".to_owned();
|
||||
|
||||
for _ in (1..3) {
|
||||
x = x + "."; //~ERROR you assign the result of adding something to this string.
|
||||
}
|
||||
|
||||
let y = "".to_owned();
|
||||
let z = y + "...";
|
||||
|
||||
assert_eq!(&x, &z);
|
||||
}
|
||||
|
||||
#[deny(string_add, string_add_assign)]
|
||||
fn both() {
|
||||
let mut x = "".to_owned();
|
||||
|
||||
for _ in (1..3) {
|
||||
|
@ -15,3 +43,14 @@ fn main() {
|
|||
|
||||
assert_eq!(&x, &z);
|
||||
}
|
||||
|
||||
fn main() {
|
||||
add_only();
|
||||
add_assign_only();
|
||||
both();
|
||||
|
||||
// the add is only caught for String
|
||||
let mut x = 1;
|
||||
x = x + 1;
|
||||
assert_eq!(2, x);
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue