Skip to content

Commit 25e90ec

Browse files
committed
Auto merge of rust-lang#8143 - GuillaumeGomez:RETURN_SELF_NOT_MUST_USE, r=xFrednet
Ensure that RETURN_SELF_NOT_MUST_USE is not emitted if the method already has `#[must_use]` Fixes rust-lang/rust-clippy#8140. --- Edit: changelog: none (The lint is not in beta yet, this should therefore not be included inside the changelog :) )
2 parents a3bf9d1 + 4da5520 commit 25e90ec

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

clippy_lints/src/return_self_not_must_use.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use rustc_hir::{Body, FnDecl, HirId, TraitItem, TraitItemKind};
55
use rustc_lint::{LateContext, LateLintPass, LintContext};
66
use rustc_middle::lint::in_external_macro;
77
use rustc_session::{declare_lint_pass, declare_tool_lint};
8-
use rustc_span::Span;
8+
use rustc_span::{sym, Span};
99

1010
declare_clippy_lint! {
1111
/// ### What it does
@@ -86,6 +86,8 @@ impl<'tcx> LateLintPass<'tcx> for ReturnSelfNotMustUse {
8686
// We are only interested in methods, not in functions or associated functions.
8787
if matches!(kind, FnKind::Method(_, _, _));
8888
if let Some(fn_def) = cx.tcx.hir().opt_local_def_id(hir_id);
89+
// We don't want to emit this lint if the `#[must_use]` attribute is already there.
90+
if !cx.tcx.hir().attrs(hir_id).iter().any(|attr| attr.has_name(sym::must_use));
8991
if let Some(impl_def) = cx.tcx.impl_of_method(fn_def.to_def_id());
9092
// We don't want this method to be te implementation of a trait because the
9193
// `#[must_use]` should be put on the trait definition directly.

tests/ui/return_self_not_must_use.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ pub struct Bar;
55

66
pub trait Whatever {
77
fn what(&self) -> Self;
8-
// There should be no warning here!
8+
// There should be no warning here! (returns a reference)
99
fn what2(&self) -> &Self;
1010
}
1111

1212
impl Bar {
13-
// There should be no warning here!
13+
// There should be no warning here! (note taking a self argument)
1414
pub fn not_new() -> Self {
1515
Self
1616
}
@@ -20,22 +20,27 @@ impl Bar {
2020
pub fn bar(self) -> Self {
2121
self
2222
}
23-
// There should be no warning here!
23+
// There should be no warning here! (private method)
2424
fn foo2(&self) -> Self {
2525
Self
2626
}
27-
// There should be no warning here!
27+
// There should be no warning here! (returns a reference)
2828
pub fn foo3(&self) -> &Self {
2929
self
3030
}
31+
// There should be no warning here! (already a `must_use` attribute)
32+
#[must_use]
33+
pub fn foo4(&self) -> Self {
34+
Self
35+
}
3136
}
3237

3338
impl Whatever for Bar {
34-
// There should be no warning here!
39+
// There should be no warning here! (comes from the trait)
3540
fn what(&self) -> Self {
3641
self.foo2()
3742
}
38-
// There should be no warning here!
43+
// There should be no warning here! (comes from the trait)
3944
fn what2(&self) -> &Self {
4045
self
4146
}

0 commit comments

Comments
 (0)