Skip to content

Commit

Permalink
Auto merge of #4880 - daxpedda:string-add, r=phansch
Browse files Browse the repository at this point in the history
Fix false positive in `string_add`.

`clippy::string_add` was popping up in macros.
I'm not sure what clippy's general direction is in these matters, but I can change it to be external macros only too.

---

changelog: Fix false positives for `string_add` in macro expansions.
  • Loading branch information
bors committed Dec 5, 2019
2 parents 374db5c + 946961d commit ff1607e
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 5 deletions.
6 changes: 5 additions & 1 deletion clippy_lints/src/strings.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use rustc::declare_lint_pass;
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::lint::{in_external_macro, LateContext, LateLintPass, LintArray, LintContext, LintPass};
use rustc_errors::Applicability;
use rustc_session::declare_tool_lint;
use syntax::source_map::Spanned;
Expand Down Expand Up @@ -80,6 +80,10 @@ declare_lint_pass!(StringAdd => [STRING_ADD, STRING_ADD_ASSIGN]);

impl<'a, 'tcx> LateLintPass<'a, 'tcx> for StringAdd {
fn check_expr(&mut self, cx: &LateContext<'a, 'tcx>, e: &'tcx Expr) {
if in_external_macro(cx.sess(), e.span) {
return;
}

if let ExprKind::Binary(
Spanned {
node: BinOpKind::Add, ..
Expand Down
8 changes: 8 additions & 0 deletions tests/ui/auxiliary/macro_rules.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,11 @@ macro_rules! try_err {
}
};
}

#[macro_export]
macro_rules! string_add {
() => {
let y = "".to_owned();
let z = y + "...";
};
}
7 changes: 7 additions & 0 deletions tests/ui/string_add.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
// aux-build:macro_rules.rs

#[macro_use]
extern crate macro_rules;

#[warn(clippy::string_add)]
#[allow(clippy::string_add_assign, unused)]
fn main() {
Expand All @@ -16,4 +21,6 @@ fn main() {
let mut x = 1;
x = x + 1;
assert_eq!(2, x);

string_add!();
}
8 changes: 4 additions & 4 deletions tests/ui/string_add.stderr
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
error: manual implementation of an assign operation
--> $DIR/string_add.rs:8:9
--> $DIR/string_add.rs:13:9
|
LL | x = x + ".";
| ^^^^^^^^^^^ help: replace it with: `x += "."`
|
= note: `-D clippy::assign-op-pattern` implied by `-D warnings`

error: you added something to a string. Consider using `String::push_str()` instead
--> $DIR/string_add.rs:8:13
--> $DIR/string_add.rs:13:13
|
LL | x = x + ".";
| ^^^^^^^
|
= note: `-D clippy::string-add` implied by `-D warnings`

error: you added something to a string. Consider using `String::push_str()` instead
--> $DIR/string_add.rs:12:13
--> $DIR/string_add.rs:17:13
|
LL | let z = y + "...";
| ^^^^^^^^^

error: manual implementation of an assign operation
--> $DIR/string_add.rs:17:5
--> $DIR/string_add.rs:22:5
|
LL | x = x + 1;
| ^^^^^^^^^ help: replace it with: `x += 1`
Expand Down

0 comments on commit ff1607e

Please sign in to comment.