Skip to content

Commit 9a150b4

Browse files
committed
Use lint_root
1 parent 24d3f5b commit 9a150b4

File tree

4 files changed

+34
-8
lines changed

4 files changed

+34
-8
lines changed

clippy_lints/src/redundant_clone.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::syntax::{
2323
source_map::{BytePos, Span},
2424
};
2525
use crate::utils::{
26-
in_macro, is_copy, match_def_path, match_type, paths, snippet_opt, span_lint, span_lint_and_then,
26+
in_macro, is_copy, match_def_path, match_type, paths, snippet_opt, span_lint_node, span_lint_node_and_then,
2727
walk_ptrs_ty_depth,
2828
};
2929
use if_chain::if_chain;
@@ -87,7 +87,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
8787
let def_id = cx.tcx.hir.body_owner_def_id(body.id());
8888
let mir = cx.tcx.optimized_mir(def_id);
8989

90-
// Looks for `call(&T)` where `T: !Copy`
90+
// Looks for `call(x: &T)` where `T: !Copy`
9191
let call = |kind: &mir::TerminatorKind<'tcx>| -> Option<(def_id::DefId, mir::Local, ty::Ty<'tcx>)> {
9292
if_chain! {
9393
if let TerminatorKind::Call { func, args, .. } = kind;
@@ -225,6 +225,12 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
225225

226226
if !used_later {
227227
let span = terminator.source_info.span;
228+
let node = if let mir::ClearCrossCrate::Set(scope_local_data) = &mir.source_scope_local_data {
229+
scope_local_data[terminator.source_info.scope].lint_root
230+
} else {
231+
unreachable!()
232+
};
233+
228234
if_chain! {
229235
if !in_macro(span);
230236
if let Some(snip) = snippet_opt(cx, span);
@@ -234,7 +240,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
234240
span.lo() + BytePos(u32::try_from(dot).unwrap())
235241
);
236242

237-
span_lint_and_then(cx, REDUNDANT_CLONE, sugg_span, "redundant clone", |db| {
243+
span_lint_node_and_then(cx, REDUNDANT_CLONE, node, sugg_span, "redundant clone", |db| {
238244
db.span_suggestion_with_applicability(
239245
sugg_span,
240246
"remove this",
@@ -247,7 +253,7 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for RedundantClone {
247253
);
248254
});
249255
} else {
250-
span_lint(cx, REDUNDANT_CLONE, span, "redundant clone");
256+
span_lint_node(cx, REDUNDANT_CLONE, node, span, "redundant clone");
251257
}
252258
}
253259
}

clippy_lints/src/utils/mod.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -562,6 +562,23 @@ pub fn span_lint_and_then<'a, 'tcx: 'a, T: LintContext<'tcx>, F>(
562562
db.docs_link(lint);
563563
}
564564

565+
pub fn span_lint_node(cx: &LateContext<'_, '_>, lint: &'static Lint, node: NodeId, sp: Span, msg: &str) {
566+
DiagnosticWrapper(cx.tcx.struct_span_lint_node(lint, node, sp, msg)).docs_link(lint);
567+
}
568+
569+
pub fn span_lint_node_and_then(
570+
cx: &LateContext<'_, '_>,
571+
lint: &'static Lint,
572+
node: NodeId,
573+
sp: Span,
574+
msg: &str,
575+
f: impl FnOnce(&mut DiagnosticBuilder<'_>),
576+
) {
577+
let mut db = DiagnosticWrapper(cx.tcx.struct_span_lint_node(lint, node, sp, msg));
578+
f(&mut db.0);
579+
db.docs_link(lint);
580+
}
581+
565582
/// Add a span lint with a suggestion on how to fix it.
566583
///
567584
/// These suggestions can be parsed by rustfix to allow it to automatically fix your code.

tests/ui/redundant_clone.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@ fn main() {
3131
let _ = OsString::new().to_owned();
3232

3333
let _ = OsString::new().to_os_string();
34+
35+
// Check that lint level works
36+
#[allow(clippy::redundant_clone)] let _ = String::new().to_string();
3437
}
3538

3639
#[derive(Clone)]

tests/ui/redundant_clone.stderr

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,15 @@ note: this value is dropped without further use
9696
| ^^^^^^^^^^^^^^^
9797

9898
error: redundant clone
99-
--> $DIR/redundant_clone.rs:40:22
99+
--> $DIR/redundant_clone.rs:43:22
100100
|
101-
40 | (a.clone(), a.clone())
101+
43 | (a.clone(), a.clone())
102102
| ^^^^^^^^ help: remove this
103103
|
104104
note: this value is dropped without further use
105-
--> $DIR/redundant_clone.rs:40:21
105+
--> $DIR/redundant_clone.rs:43:21
106106
|
107-
40 | (a.clone(), a.clone())
107+
43 | (a.clone(), a.clone())
108108
| ^
109109

110110
error: aborting due to 9 previous errors

0 commit comments

Comments
 (0)