Skip to content

Commit aa7bcb9

Browse files
committed
Don't expand macro in identity_conversion suggestion
1 parent 457e7f1 commit aa7bcb9

File tree

4 files changed

+17
-3
lines changed

4 files changed

+17
-3
lines changed

clippy_lints/src/identity_conversion.rs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
1212
use crate::rustc::{declare_tool_lint, lint_array};
1313
use crate::rustc::hir::*;
1414
use crate::syntax::ast::NodeId;
15-
use crate::utils::{in_macro, match_def_path, match_trait_method, same_tys, snippet, span_lint_and_then};
15+
use crate::utils::{in_macro, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then};
1616
use crate::utils::{opt_def_id, paths, resolve_node};
1717
use crate::rustc_errors::Applicability;
1818

@@ -72,7 +72,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
7272
let a = cx.tables.expr_ty(e);
7373
let b = cx.tables.expr_ty(&args[0]);
7474
if same_tys(cx, a, b) {
75-
let sugg = snippet(cx, args[0].span, "<expr>").into_owned();
75+
let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
76+
7677
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
7778
db.span_suggestion_with_applicability(
7879
e.span,

clippy_lints/src/utils/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -362,6 +362,12 @@ pub fn snippet<'a, 'b, T: LintContext<'b>>(cx: &T, span: Span, default: &'a str)
362362
snippet_opt(cx, span).map_or_else(|| Cow::Borrowed(default), From::from)
363363
}
364364

365+
/// Same as `snippet`, but should only be used when it's clear that the input span is
366+
/// not a macro argument.
367+
pub fn snippet_with_macro_callsite<'a, 'b, T: LintContext<'b>>(cx: &T, span: Span, default: &'a str) -> Cow<'a, str> {
368+
snippet(cx, span.source_callsite(), default)
369+
}
370+
365371
/// Convert a span to a code snippet. Returns `None` if not available.
366372
pub fn snippet_opt<'a, T: LintContext<'a>>(cx: &T, span: Span) -> Option<String> {
367373
cx.sess().source_map().span_to_snippet(span).ok()

tests/ui/identity_conversion.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,5 @@ fn main() {
5353
let _ = String::from(format!("A: {:04}", 123));
5454
let _ = "".lines().into_iter();
5555
let _ = vec![1, 2, 3].into_iter().into_iter();
56+
let _: String = format!("Hello {}", "world").into();
5657
}

tests/ui/identity_conversion.stderr

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,11 @@ error: identical conversion
5858
55 | let _ = vec![1, 2, 3].into_iter().into_iter();
5959
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()`
6060

61-
error: aborting due to 9 previous errors
61+
error: identical conversion
62+
--> $DIR/identity_conversion.rs:56:21
63+
|
64+
56 | let _: String = format!("Hello {}", "world").into();
65+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")`
66+
67+
error: aborting due to 10 previous errors
6268

0 commit comments

Comments
 (0)