Use lint_root

This commit is contained in:
Shotaro Yamada 2018-10-25 21:08:32 +09:00
parent 24d3f5b48f
commit 9a150b4aa1
4 changed files with 34 additions and 8 deletions

View file

@ -23,7 +23,7 @@ use crate::syntax::{
source_map::{BytePos, Span},
};
use crate::utils::{
in_macro, is_copy, match_def_path, match_type, paths, snippet_opt, span_lint, span_lint_and_then,
in_macro, is_copy, match_def_path, match_type, paths, snippet_opt, span_lint_node, span_lint_node_and_then,
walk_ptrs_ty_depth,
};
use if_chain::if_chain;
@ -87,7 +87,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
let def_id = cx.tcx.hir.body_owner_def_id(body.id());
let mir = cx.tcx.optimized_mir(def_id);
// Looks for `call(&T)` where `T: !Copy`
// Looks for `call(x: &T)` where `T: !Copy`
let call = |kind: &mir::TerminatorKind<'tcx>| -> Option<(def_id::DefId, mir::Local, ty::Ty<'tcx>)> {
if_chain! {
if let TerminatorKind::Call { func, args, .. } = kind;
@ -225,6 +225,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
if !used_later {
let span = terminator.source_info.span;
let node = if let mir::ClearCrossCrate::Set(scope_local_data) = &mir.source_scope_local_data {
scope_local_data[terminator.source_info.scope].lint_root
} else {
unreachable!()
};
if_chain! {
if !in_macro(span);
if let Some(snip) = snippet_opt(cx, span);
@ -234,7 +240,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
span.lo() + BytePos(u32::try_from(dot).unwrap())
);
span_lint_and_then(cx, REDUNDANT_CLONE, sugg_span, "redundant clone", |db| {
span_lint_node_and_then(cx, REDUNDANT_CLONE, node, sugg_span, "redundant clone", |db| {
db.span_suggestion_with_applicability(
sugg_span,
"remove this",
@ -247,7 +253,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
);
});
} else {
span_lint(cx, REDUNDANT_CLONE, span, "redundant clone");
span_lint_node(cx, REDUNDANT_CLONE, node, span, "redundant clone");
}
}
}

View file

@ -562,6 +562,23 @@ pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>(
db.docs_link(lint);
}
pub fn span_lint_node(cx: &LateContext<'_, '_>, lint: &'static Lint, node: NodeId, sp: Span, msg: &str) {
DiagnosticWrapper(cx.tcx.struct_span_lint_node(lint, node, sp, msg)).docs_link(lint);
}
pub fn span_lint_node_and_then(
cx: &LateContext<'_, '_>,
lint: &'static Lint,
node: NodeId,
sp: Span,
msg: &str,
f: impl FnOnce(&mut DiagnosticBuilder<'_>),
) {
let mut db = DiagnosticWrapper(cx.tcx.struct_span_lint_node(lint, node, sp, msg));
f(&mut db.0);
db.docs_link(lint);
}
/// Add a span lint with a suggestion on how to fix it.
///
/// These suggestions can be parsed by rustfix to allow it to automatically fix your code.

View file

@ -31,6 +31,9 @@ fn main() {
let _ = OsString::new().to_owned();
let _ = OsString::new().to_os_string();
// Check that lint level works
#[allow(clippy::redundant_clone)] let _ = String::new().to_string();
}
#[derive(Clone)]

View file

@ -96,15 +96,15 @@ note: this value is dropped without further use
| ^^^^^^^^^^^^^^^
error: redundant clone
--> $DIR/redundant_clone.rs:40:22
--> $DIR/redundant_clone.rs:43:22
|
40 | (a.clone(), a.clone())
43 | (a.clone(), a.clone())
| ^^^^^^^^ help: remove this
|
note: this value is dropped without further use
--> $DIR/redundant_clone.rs:40:21
--> $DIR/redundant_clone.rs:43:21
|
40 | (a.clone(), a.clone())
43 | (a.clone(), a.clone())
| ^
error: aborting due to 9 previous errors