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

dependency_on_unit_never_type_fallback suggests adding type param annotation inside a format string #136562

Closed
nskobelevs opened this issue Feb 5, 2025 · 3 comments · Fixed by #136598
Assignees
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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

@nskobelevs
Copy link

The dependency_on_unit_never_type_fallback lint seems to suggest to add a ::<()> type annotation inside a format string in some cases.

Example:

fn create_ok_default<C>() -> Result<C, ()>
where
    C: Default,
{
    Ok(C::default())
}

fn main() -> Result<(), ()> {
    let (returned_value, _) = (|| {
        let created = create_ok_default()?;
        Ok((created, ()))
    })()?;

    let _ = format_args!("{:?}", returned_value);
    Ok(())
}

Suggests the following changes

@@ -11,6 +11,6 @@ fn main() -> Result<(), ()> {
         Ok((created, ()))
     })()?;

-    let _ = format_args!("{:?}", returned_value);
+    let _ = format_args!("{:?}::<()>", returned_value);
     Ok(())
 }
Raw Output

warning: this function depends on never type fallback being `()`
  --> src/main.rs:8:1
   |
8  | fn main() -> Result<(), ()> {
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = warning: this was previously accepted by the compiler but is being phased out; it will become a hard error in Rust 2024 and in a future release in all editions!
   = note: for more information, see issue #123748 <https://github.com/rust-lang/rust/issues/123748>
   = help: specify the types explicitly
note: in edition 2024, the requirement `!: std::default::Default` will fail
  --> src/main.rs:10:23
   |
10 |         let created = create_ok_default()?;
   |                       ^^^^^^^^^^^^^^^^^^^
   = note: `#[warn(dependency_on_unit_never_type_fallback)]` on by default
help: use `()` annotations to avoid fallback changes
   |
14 |     let _ = format_args!("{:?}::<()>::<()>", returned_value);
   |                               ++++++

warning: this let-binding has unit value
  --> src/main.rs:10:9
   |
10 |         let created = create_ok_default()?;
   |         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#let_unit_value
   = note: `#[warn(clippy::let_unit_value)]` on by default
help: omit the `let` binding
   |
10 |         create_ok_default()?;
   |
help: variable `created` of type `()` can be replaced with explicit `()`
   |
11 |         Ok(((), ()))
   |             ~~

warning: `tmp` (bin "tmp") generated 2 warnings (run `cargo clippy --fix --bin "tmp"` to apply 2 suggestions)
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.00s

Meta

rustc --version --verbose:

rustc 1.84.1 (e71f9a9a9 2025-01-27)
binary: rustc
commit-hash: e71f9a9a98b0faf423844bf0ba7438f29dc27d58
commit-date: 2025-01-27
host: aarch64-apple-darwin
release: 1.84.1
LLVM version: 19.1.5

This suggestion is not given on 1.83.0

@nskobelevs nskobelevs added the C-bug Category: This is a bug. label Feb 5, 2025
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Feb 5, 2025
@nskobelevs
Copy link
Author

nskobelevs commented Feb 5, 2025

Contrived example but first came across it using winnow crate so this was my best attempt at simplifying it to a small example.

The tuple part seems relevant since the same suggestion is not given for non-tuple return from the closure. Similarly this does not seem to occur without the closure surrounding it.

I'm also not particularly sure if this might be two separate issues - the incorrect suggestion and the type inference?

It feels like this should return a complication error that the type parameter C cannot be inferred but my editor hints show the call to create_ok_default having C = ! (which afaiu ends up falling back to () for returned_value) which is even more unexpected since ! doesn't implement Default (and as per docs it wouldn't make sense for it to).

@nskobelevs
Copy link
Author

nskobelevs commented Feb 5, 2025

cc @WaffleLapkin (saw you getting tagged on previous issues for this lint so assuming your eyes are relevant here & it's okay to tag you directly)

@fmease fmease added A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` D-invalid-suggestion Diagnostics: A structured suggestion resulting in incorrect code. A-diagnostics Area: Messages for errors, warnings, and lints 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. L-dependency_on_unit_never_type_fallback Lint: dependency_on_unit_never_type_fallback and removed C-bug Category: This is a bug. needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. A-diagnostics Area: Messages for errors, warnings, and lints labels Feb 5, 2025
@compiler-errors
Copy link
Member

probably more appropriate to tag me bc I'm the one who implemented the suggestion. I'll look into it briefly, but any fix for this is almost certainly not going to land in 1.85 with the new edition.

@compiler-errors compiler-errors self-assigned this Feb 5, 2025
matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Feb 7, 2025
…affleLapkin

Fix suggestion for `dependency_on_unit_never_type_fallback` involving closures + format args expansions

fixes rust-lang#136562

r? wafflelapkin or reassign
@bors bors closed this as completed in 7489e56 Feb 7, 2025
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Feb 7, 2025
Rollup merge of rust-lang#136598 - compiler-errors:unit-fallback, r=WaffleLapkin

Fix suggestion for `dependency_on_unit_never_type_fallback` involving closures + format args expansions

fixes rust-lang#136562

r? wafflelapkin or reassign
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lints Area: Lints (warnings about flaws in source code) such as unused_mut. A-macros Area: All kinds of macros (custom derive, macro_rules!, proc macros, ..) A-suggestion-diagnostics Area: Suggestions generated by the compiler applied by `cargo fix` 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
4 participants