Skip to content

Commit 4127c23

Browse files
authored
Merge pull request #2168 from cgm616/master
Prevent linting should_implement_trait on private methods
2 parents 47be692 + 3902b83 commit 4127c23

File tree

3 files changed

+201
-196
lines changed

3 files changed

+201
-196
lines changed

clippy_lints/src/methods.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -760,15 +760,17 @@ impl<'a, 'tcx> LateLintPass<'a, 'tcx> for Pass {
760760
if let Some(first_arg) = iter_input_pats(&sig.decl, cx.tcx.hir.body(id)).next();
761761
if let hir::ItemImpl(_, _, _, _, None, ref self_ty, _) = item.node;
762762
then {
763+
if cx.access_levels.is_exported(implitem.id) {
763764
// check missing trait implementations
764-
for &(method_name, n_args, self_kind, out_type, trait_name) in &TRAIT_METHODS {
765-
if name == method_name &&
766-
sig.decl.inputs.len() == n_args &&
767-
out_type.matches(&sig.decl.output) &&
768-
self_kind.matches(first_arg_ty, first_arg, self_ty, false, &implitem.generics) {
769-
span_lint(cx, SHOULD_IMPLEMENT_TRAIT, implitem.span, &format!(
770-
"defining a method called `{}` on this type; consider implementing \
771-
the `{}` trait or choosing a less ambiguous name", name, trait_name));
765+
for &(method_name, n_args, self_kind, out_type, trait_name) in &TRAIT_METHODS {
766+
if name == method_name &&
767+
sig.decl.inputs.len() == n_args &&
768+
out_type.matches(&sig.decl.output) &&
769+
self_kind.matches(first_arg_ty, first_arg, self_ty, false, &implitem.generics) {
770+
span_lint(cx, SHOULD_IMPLEMENT_TRAIT, implitem.span, &format!(
771+
"defining a method called `{}` on this type; consider implementing \
772+
the `{}` trait or choosing a less ambiguous name", name, trait_name));
773+
}
772774
}
773775
}
774776

tests/ui/methods.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,14 @@ use std::iter::FromIterator;
1414
use std::rc::{self, Rc};
1515
use std::sync::{self, Arc};
1616

17-
struct T;
17+
pub struct T;
1818

1919
impl T {
20-
fn add(self, other: T) -> T { self }
21-
fn drop(&mut self) { }
20+
pub fn add(self, other: T) -> T { self }
21+
22+
pub(crate) fn drop(&mut self) { } // no error, not public interfact
23+
fn neg(self) -> Self { self } // no error, private function
24+
fn eq(&self, other: T) -> bool { true } // no error, private function
2225

2326
fn sub(&self, other: T) -> &T { self } // no error, self is a ref
2427
fn div(self) -> T { self } // no error, different #arguments

0 commit comments

Comments
 (0)