Skip to content
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

False positive E0308 when iterating and matching enum items from an impl Into #16657

Closed
Enet4 opened this issue Feb 24, 2024 · 1 comment
Closed
Labels
C-bug Category: bug

Comments

@Enet4
Copy link

Enet4 commented Feb 24, 2024

The following code compiles without issues in the latest stable Rust, but rust-analyzer makes some incorrect type inference assumptions which lead to error E0308.

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
struct Tag(u32);

#[allow(dead_code)]
enum SelectorStep {
    Bar(Tag),
    Baz { tag: Tag, item: u32 },
}

struct Selector {
    steps: Vec<SelectorStep>,
}

impl Selector {
    fn iter(&self) -> impl Iterator<Item = &SelectorStep> {
        self.steps.iter()
    }
}

#[allow(dead_code)]
fn process_steps(
    selector: impl Into<Selector>,
) {
    let selector = selector.into();
    let mut map = std::collections::HashMap::new();

    for (i, step) in selector.iter().enumerate() {
        match step {
            SelectorStep::Bar(tag) => {
                map.insert(*tag, i);
            },
            SelectorStep::Baz { tag, .. } => {
                let x = map.get(tag);
                println!("Baz {x:?}");
            }
        }
    }
}

fn main() {}
expected &{unknown}, found Tag     rust-analyzer(E0308) [Ln 33, Col 33]
[{
	"resource": ".../use-clippy-wat/src/main.rs",
	"owner": "rustc",
	"code": {
		"value": "E0308",
		"target": {
			"$mid": 1,
			"external": "https://doc.rust-lang.org/stable/error_codes/E0308.html",
			"path": "/stable/error_codes/E0308.html",
			"scheme": "https",
			"authority": "doc.rust-lang.org"
		}
	},
	"severity": 8,
	"message": "expected &{unknown}, found Tag",
	"source": "rust-analyzer",
	"startLineNumber": 33,
	"startColumn": 33,
	"endLineNumber": 33,
	"endColumn": 36
}]

Following the suggestion to add & before tag in let x = map.get(tag); will trigger the needless_borrow Clippy warning.

rust-analyzer version: both 0.4.1857-standalone and 0.3.1850-standalone reproduce this

rustc version: rustc 1.76.0 (07dca489a 2024-02-04)

relevant settings: 🤷

@Enet4 Enet4 added the C-bug Category: bug label Feb 24, 2024
@flodiebold
Copy link
Member

flodiebold commented Feb 24, 2024

Duplicate of #5514 (which means it's likely to only be solved with the new trait solver)

@flodiebold flodiebold marked this as a duplicate of #5514 Feb 24, 2024
@flodiebold flodiebold closed this as not planned Won't fix, can't repro, duplicate, stale Feb 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: bug
Projects
None yet
Development

No branches or pull requests

2 participants