Skip to content

Commit

Permalink
Avoid false positives from extension traits
Browse files Browse the repository at this point in the history
  • Loading branch information
schubart committed Mar 6, 2023
1 parent fbb7fd5 commit 85ad8a6
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
6 changes: 6 additions & 0 deletions clippy_lints/src/collection_is_never_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,16 @@ fn has_no_read_access<'tcx>(cx: &LateContext<'tcx>, id: HirId, block: &'tcx Bloc
// Method call on `id` in a statement ignores any return value, so it's not a read access:
//
// id.foo(...); // Not reading `id`.
//
// Only assuming this for "official" methods defined on the type. For methods defined in extension
// traits (identified as local, based on the orphan rule), pessimistically assume that they might
// have side effects, so consider them a read.
if let Some(Node::Expr(parent)) = get_parent_node(cx.tcx, expr.hir_id)
&& let ExprKind::MethodCall(_, receiver, _, _) = parent.kind
&& path_to_local_id(receiver, id)
&& let Some(Node::Stmt(..)) = get_parent_node(cx.tcx, parent.hir_id)
&& let Some(method_def_id) = cx.typeck_results().type_dependent_def_id(parent.hir_id)
&& !method_def_id.is_local()
{
return ControlFlow::Continue(());
}
Expand Down
20 changes: 20 additions & 0 deletions tests/ui/collection_is_never_read.rs
Original file line number Diff line number Diff line change
Expand Up @@ -133,3 +133,23 @@ fn not_read_if_return_value_not_used() {
let x = vec![1, 2, 3]; // WARNING
x.is_empty();
}

fn extension_traits() {
trait VecExt<T> {
fn method_with_side_effect(&self);
fn method_without_side_effect(&self);
}

impl<T> VecExt<T> for Vec<T> {
fn method_with_side_effect(&self) {
println!("my length: {}", self.len());
}
fn method_without_side_effect(&self) {}
}

let x = vec![1, 2, 3]; // Ok
x.method_with_side_effect();

let y = vec![1, 2, 3]; // Ok (false negative)
y.method_without_side_effect();
}

0 comments on commit 85ad8a6

Please sign in to comment.