Skip to content

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

Closed
@nskobelevs

Description

@nskobelevs

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

Metadata

Metadata

Labels

A-lintsArea: Lints (warnings about flaws in source code) such as unused_mut.A-macrosArea: All kinds of macros (custom derive, macro_rules!, proc macros, ..)A-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-invalid-suggestionDiagnostics: A structured suggestion resulting in incorrect code.L-dependency_on_unit_never_type_fallbackLint: dependency_on_unit_never_type_fallbackT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions