Skip to content

Ensure that RETURN_SELF_NOT_MUST_USE is not emitted if the method already has #[must_use] #8143

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Dec 18, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion clippy_lints/src/return_self_not_must_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use rustc_hir::{Body, FnDecl, HirId, TraitItem, TraitItemKind};
use rustc_lint::{LateContext, LateLintPass, LintContext};
use rustc_middle::lint::in_external_macro;
use rustc_session::{declare_lint_pass, declare_tool_lint};
use rustc_span::Span;
use rustc_span::{sym, Span};

declare_clippy_lint! {
/// ### What it does
Expand Down Expand Up @@ -86,6 +86,8 @@ impl<'tcx> LateLintPass<'tcx> for ReturnSelfNotMustUse {
// We are only interested in methods, not in functions or associated functions.
if matches!(kind, FnKind::Method(_, _, _));
if let Some(fn_def) = cx.tcx.hir().opt_local_def_id(hir_id);
// We don't want to emit this lint if the `#[must_use]` attribute is already there.
if !cx.tcx.hir().attrs(hir_id).iter().any(|attr| attr.has_name(sym::must_use));
if let Some(impl_def) = cx.tcx.impl_of_method(fn_def.to_def_id());
// We don't want this method to be te implementation of a trait because the
// `#[must_use]` should be put on the trait definition directly.
Expand Down
17 changes: 11 additions & 6 deletions tests/ui/return_self_not_must_use.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@ pub struct Bar;

pub trait Whatever {
fn what(&self) -> Self;
// There should be no warning here!
// There should be no warning here! (returns a reference)
fn what2(&self) -> &Self;
}

impl Bar {
// There should be no warning here!
// There should be no warning here! (note taking a self argument)
pub fn not_new() -> Self {
Self
}
Expand All @@ -20,22 +20,27 @@ impl Bar {
pub fn bar(self) -> Self {
self
}
// There should be no warning here!
// There should be no warning here! (private method)
fn foo2(&self) -> Self {
Self
}
// There should be no warning here!
// There should be no warning here! (returns a reference)
pub fn foo3(&self) -> &Self {
self
}
// There should be no warning here! (already a `must_use` attribute)
#[must_use]
pub fn foo4(&self) -> Self {
Self
}
}

impl Whatever for Bar {
// There should be no warning here!
// There should be no warning here! (comes from the trait)
fn what(&self) -> Self {
self.foo2()
}
// There should be no warning here!
// There should be no warning here! (comes from the trait)
fn what2(&self) -> &Self {
self
}
Expand Down