Skip to content

equatable_if_let suggests wrong code when it involves a reference #8710

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

Open
sozysozbot opened this issue Apr 16, 2022 · 1 comment · May be fixed by #14504
Open

equatable_if_let suggests wrong code when it involves a reference #8710

sozysozbot opened this issue Apr 16, 2022 · 1 comment · May be fixed by #14504
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-nursery Lint: Currently in the nursery group

Comments

@sozysozbot
Copy link
Contributor

sozysozbot commented Apr 16, 2022

Summary

equatable_if_let tries to replace a pattern into a ==, but the two differ slightly in their semantics when it comes to reference. Hence the current implementation creates a suggestion that causes a type mismatch error when adopted.

Reproducer

I tried this code:

#![warn(clippy::pedantic, clippy::nursery)]
fn bar() {todo!()}
fn baz() {todo!()}

pub fn foo(cs: &[char]) {
    let mut iter = cs.iter();
    if let Some('i') = iter.next() {
        bar();
    } else {
        baz();
    }
}

I expected to see this happen: Clippy suggests

try: `iter.next() == Some(&'i')`

Instead, this happened: Clippy suggested

try: `iter.next() == Some('i')`

which is a wrong suggestion because replacing the original code with this snippet causes a type error.

Version

rustc 1.60.0 (7737e0b5c 2022-04-04)
binary: rustc
commit-hash: 7737e0b5c4103216d6fd8cf941b7ab9bdbaace7c
commit-date: 2022-04-04
host: x86_64-pc-windows-msvc
release: 1.60.0
LLVM version: 14.0.0

Additional Labels

@rustbot label +l-suggestion-causes-error

@sozysozbot sozysozbot added the C-bug Category: Clippy is not doing the correct thing label Apr 16, 2022
@rustbot

This comment was marked as off-topic.

@xFrednet xFrednet added the I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied label Apr 17, 2022
@J-ZhengLi J-ZhengLi added the L-nursery Lint: Currently in the nursery group label Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied L-nursery Lint: Currently in the nursery group
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants