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

Bad suggestion for dependency_on_unit_never_type_fallback for wildcard assignment expression #133688

Closed
ehuss opened this issue Nov 30, 2024 · 1 comment · Fixed by #133691
Closed
Assignees
Labels
A-edition-2024 Area: The 2024 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-dependency_on_unit_never_type_fallback Lint: dependency_on_unit_never_type_fallback T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@ehuss
Copy link
Contributor

ehuss commented Nov 30, 2024

The dependency_on_unit_never_type_fallback lint seems to provide a bad suggestion for an assignment to a wildcard pattern. Example (sorry, not minimized further, this is based on serde):

#![deny(dependency_on_unit_never_type_fallback)]

pub trait Deserialize: Sized {
    fn deserialize<D>(deserializer: D) -> Result<Self, ()>
    where
        D: Deserializer;
}

pub trait Deserializer: Sized {}

pub trait MapAccess {
    fn next_value<V>(&mut self) -> Result<V, ()>
    where
        V: Deserialize;
}

impl Deserialize for () {
    fn deserialize<D>(_: D) -> Result<Self, ()>
    where
        D: Deserializer,
    {
        Ok(())
    }
}

fn visit_map<A>(mut map: A) -> Result<i32, ()>
where
    A: MapAccess,
{
    _ = map.next_value()?;
    Ok(1)
}

This gives a suggestion to modify it with:

@@ -29,7 +29,7 @@
 where
     A: MapAccess,
 {
-    _ = map.next_value()?;
+    _: () = map.next_value()?;
     Ok(1)
 }

This is not valid syntax, as an assignment expression does not allow qualifying with types.

In this case, I'm uncertain if the fallback can actually be a problem? If not, then maybe the lint shouldn't fire? Or if a type annotation is needed, then maybe use fully-qualified syntax?

cc @WaffleLapkin

@ehuss ehuss added A-edition-2024 Area: The 2024 edition D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-dependency_on_unit_never_type_fallback Lint: dependency_on_unit_never_type_fallback labels Nov 30, 2024
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Nov 30, 2024
@jieyouxu jieyouxu added T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Dec 1, 2024
@compiler-errors
Copy link
Member

Never type fallback definitely matters in this case. In edition 2024, this code fails to compile.

I'll fix the suggestion.

@compiler-errors compiler-errors self-assigned this Dec 1, 2024
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Dec 1, 2024
Check let source before suggesting annotation

Make sure we don't annotate nonsense type annotations on locals that come from desugarings.

fixes rust-lang#133688
@bors bors closed this as completed in 78dad1e Dec 1, 2024
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Dec 1, 2024
Rollup merge of rust-lang#133691 - compiler-errors:let-source, r=lqd

Check let source before suggesting annotation

Make sure we don't annotate nonsense type annotations on locals that come from desugarings.

fixes rust-lang#133688
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-edition-2024 Area: The 2024 edition A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-edition Diagnostics: An error or lint that should account for edition differences. D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. L-dependency_on_unit_never_type_fallback Lint: dependency_on_unit_never_type_fallback T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants