ref_binding_to_reference false negative in expressions misclassified as postfix #12990
Labels
C-bug
Category: Clippy is not doing the correct thing
I-false-negative
Issue: The lint should have been triggered on code, but wasn't
Summary
I noticed this surprising code because I am working on removing
PREC_POSTFIX
fromrustc_ast
.rust-clippy/clippy_lints/src/dereference.rs
Lines 1157 to 1164 in 32374a1
The point of the above condition is to distinguish between 2 cases:
In the first
if let
/debug
, Clippy wants to suggest that we remove theref
and change the next line to replacex
with&x
to preserve the meaning of the program.In the second
if let
/debug
, Clippy does not want to suggest removing theref
because changingdebug(x[false])
todebug((&x)[false])
would require one more level of parentheses to preserve the meaning of the program than was needed before. Without theref
,debug(&x[false])
anddebug(x[false])
both mean something different than what the program did originally, and neither one compiles.Everything described so far is behaving correctly.
The bug is that Clippy is trying to draw the above distinction based on the precedence of
get_parent_expr(cx, e)
being postfix, which doesn't make sense.When evaluating whether to suggest removing the
ref
on aref x
, thex
in the following expressions all have a parent expression kind with postfix precedence:x
x.await
x()
f(x)
x[i]
other[x]
x.f()
other.f(x)
x?
However, the parent expression kind being a postfix expression is not the right condition for the purpose of the
ref_binding_to_reference
lint. The relevant thing is whetherx
's parent expression is a postfix ofx
specifically.x()
andf(x)
are both Call expressions, butf(&x)
is okay to suggest while(&x)()
is not.Of the expressions in the table, Clippy has a false negative on
f(x)
andother[x]
andother.f(x)
.Lint Name
ref_binding_to_reference
Reproducer
Version
The text was updated successfully, but these errors were encountered: