Don't expand macro in identity_conversion suggestion

This commit is contained in:
Philipp Hansch 2018-10-27 11:01:27 +02:00
parent 457e7f12e9
commit aa7bcb9074
No known key found for this signature in database
GPG Key ID: B6FA06A6E0E2665B
4 changed files with 17 additions and 3 deletions

View File

@ -12,7 +12,7 @@ use crate::rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use crate::rustc::{declare_tool_lint, lint_array}; use crate::rustc::{declare_tool_lint, lint_array};
use crate::rustc::hir::*; use crate::rustc::hir::*;
use crate::syntax::ast::NodeId; use crate::syntax::ast::NodeId;
use crate::utils::{in_macro, match_def_path, match_trait_method, same_tys, snippet, span_lint_and_then}; use crate::utils::{in_macro, match_def_path, match_trait_method, same_tys, snippet, snippet_with_macro_callsite, span_lint_and_then};
use crate::utils::{opt_def_id, paths, resolve_node}; use crate::utils::{opt_def_id, paths, resolve_node};
use crate::rustc_errors::Applicability; use crate::rustc_errors::Applicability;
@ -72,7 +72,8 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for IdentityConversion {
let a = cx.tables.expr_ty(e); let a = cx.tables.expr_ty(e);
let b = cx.tables.expr_ty(&args[0]); let b = cx.tables.expr_ty(&args[0]);
if same_tys(cx, a, b) { if same_tys(cx, a, b) {
let sugg = snippet(cx, args[0].span, "<expr>").into_owned(); let sugg = snippet_with_macro_callsite(cx, args[0].span, "<expr>").to_string();
span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| { span_lint_and_then(cx, IDENTITY_CONVERSION, e.span, "identical conversion", |db| {
db.span_suggestion_with_applicability( db.span_suggestion_with_applicability(
e.span, e.span,

View File

@ -362,6 +362,12 @@ pub fn snippet<'a, 'b, T: LintContext<'b>>(cx: &T, span: Span, default: &'a str)
snippet_opt(cx, span).map_or_else(|| Cow::Borrowed(default), From::from) snippet_opt(cx, span).map_or_else(|| Cow::Borrowed(default), From::from)
} }
/// Same as `snippet`, but should only be used when it's clear that the input span is
/// not a macro argument.
pub fn snippet_with_macro_callsite<'a, 'b, T: LintContext<'b>>(cx: &T, span: Span, default: &'a str) -> Cow<'a, str> {
snippet(cx, span.source_callsite(), default)
}
/// Convert a span to a code snippet. Returns `None` if not available. /// Convert a span to a code snippet. Returns `None` if not available.
pub fn snippet_opt<'a, T: LintContext<'a>>(cx: &T, span: Span) -> Option<String> { pub fn snippet_opt<'a, T: LintContext<'a>>(cx: &T, span: Span) -> Option<String> {
cx.sess().source_map().span_to_snippet(span).ok() cx.sess().source_map().span_to_snippet(span).ok()

View File

@ -53,4 +53,5 @@ fn main() {
let _ = String::from(format!("A: {:04}", 123)); let _ = String::from(format!("A: {:04}", 123));
let _ = "".lines().into_iter(); let _ = "".lines().into_iter();
let _ = vec![1, 2, 3].into_iter().into_iter(); let _ = vec![1, 2, 3].into_iter().into_iter();
let _: String = format!("Hello {}", "world").into();
} }

View File

@ -58,5 +58,11 @@ error: identical conversion
55 | let _ = vec![1, 2, 3].into_iter().into_iter(); 55 | let _ = vec![1, 2, 3].into_iter().into_iter();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into_iter()`: `vec![1, 2, 3].into_iter()`
error: aborting due to 9 previous errors error: identical conversion
--> $DIR/identity_conversion.rs:56:21
|
56 | let _: String = format!("Hello {}", "world").into();
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider removing `.into()`: `format!("Hello {}", "world")`
error: aborting due to 10 previous errors