rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

manual_map sugg with dyn causes error

Open gepbird opened this issue 10 months ago • 2 comments

Reproducer

This code compiles:

trait DummyTrait {}

fn foo<P, T>(function: P)
where
    P: FnOnce() -> Result<T, String>,
    T: DummyTrait,
{
    let _: Option<Result<Box<dyn DummyTrait>, String>> = match Some(()) {
        Some(_) => Some(match function() {
            Ok(value) => Ok(Box::new(value)),
            _ => todo!(),
        }),
        None => None,
    };
}

But when applying the manual map suggestion, it no longer compiles:

error[E0308]: mismatched types
  --> src/lib.rs:8:58
   |
3  |   fn foo<P, T>(function: P)
   |             - found this type parameter
...
8  |       let _: Option<Result<Box<dyn DummyTrait>, String>> = Some(()).map(|_| match function() {
   |  ____________-------------------------------------------___^
   | |            |
   | |            expected due to this
9  | |             Ok(value) => Ok(Box::new(value)),
10 | |             _ => todo!(),
11 | |         });
   | |__________^ expected `Option<Result<Box<...>, ...>>`, found `Option<Result<Box<T>, _>>`
   |
   = note: expected enum `std::option::Option<std::result::Result<std::boxed::Box<dyn DummyTrait>, std::string::Stri
ng>>`
              found enum `std::option::Option<std::result::Result<std::boxed::Box<T>, _>>`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

This was the most minimal example I could make to reproduce the issue.

The project where I got this problem:

This code compiles

Full output of clippy:

    Checking timetable-optimizer-web v0.1.0 (/home/gep/timetable-optimizer/web)
    Checking timetable-optimizer-cli v0.1.0 (/home/gep/timetable-optimizer/cli)
    Checking timetable-optimizer-lib v0.1.0 (/home/gep/timetable-optimizer/lib)
warning: failed to automatically apply fixes suggested by rustc to crate `timetable_optimizer_cli`

after fixes were automatically applied the compiler reported errors within these files:

  * cli/src/filter/mod.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0308]: mismatched types
  --> cli/src/filter/mod.rs:25:3
   |
16 |   fn parse_with_key<F, T>(
   |                        - found this type parameter
...
20 |   ) -> Option<Result<Box<dyn Filter>, String>>
   |        --------------------------------------- expected `std::option::Option<std::result::Result<std::boxed::Box<(dyn filter::Filter + 'static)>, std::string::String>>` because of re
turn type
...
25 | /   spec.strip_prefix(&(key.to_string() + "=")).map(|value| match parse_fn(value) {
26 | |       Ok(filter) => Ok(Box::new(filter)),
27 | |       Err(e) => Err(e),
28 | |     })
   | |______^ expected `Option<Result<Box<dyn Filter>, String>>`, found `Option<Result<Box<T>, String>>`
   |
   = note: expected enum `std::option::Option<std::result::Result<std::boxed::Box<(dyn filter::Filter + 'static)>, _>>`
              found enum `std::option::Option<std::result::Result<std::boxed::Box<T>, _>>`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: manual implementation of `Option::map`
  --> cli/src/filter/mod.rs:25:3
   |
25 | /   match spec.strip_prefix(&(key.to_string() + "=")) {
26 | |     Some(value) => Some(match parse_fn(value) {
27 | |       Ok(filter) => Ok(Box::new(filter)),
28 | |       Err(e) => Err(e),
29 | |     }),
30 | |     None => None,
31 | |   }
   | |___^
   |
   = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#manual_map
   = note: `#[warn(clippy::manual_map)]` on by default
help: try
   |
25 ~   spec.strip_prefix(&(key.to_string() + "=")).map(|value| match parse_fn(value) {
26 +       Ok(filter) => Ok(Box::new(filter)),
27 +       Err(e) => Err(e),
28 +     })
   |

warning: `timetable-optimizer-cli` (bin "timetable-optimizer-cli") generated 1 warning (run `cargo clippy --fix --bin "timetable-optimizer-cli"` to apply 1 suggestion)
warning: failed to automatically apply fixes suggested by rustc to crate `timetable_optimizer_cli`

after fixes were automatically applied the compiler reported errors within these files:

  * cli/src/filter/mod.rs

This likely indicates a bug in either rustc or cargo itself,
and we would appreciate a bug report! You're likely to see
a number of compiler warnings after this message which cargo
attempted to fix but failed. If you could open an issue at
https://github.com/rust-lang/rust-clippy/issues
quoting the full output of this command we'd be very appreciative!
Note that you may be able to make some more progress in the near-term
fixing code with the `--broken-code` flag

The following errors were reported:
error[E0308]: mismatched types
  --> cli/src/filter/mod.rs:25:3
   |
16 |   fn parse_with_key<F, T>(
   |                        - found this type parameter
...
20 |   ) -> Option<Result<Box<dyn Filter>, String>>
   |        --------------------------------------- expected `std::option::Option<std::result::Result<std::boxed::Box<(dyn filter::Filter + 'static)>, std::string::String>>` because of re
turn type
...
25 | /   spec.strip_prefix(&(key.to_string() + "=")).map(|value| match parse_fn(value) {
26 | |       Ok(filter) => Ok(Box::new(filter)),
27 | |       Err(e) => Err(e),
28 | |     })
   | |______^ expected `Option<Result<Box<dyn Filter>, String>>`, found `Option<Result<Box<T>, String>>`
   |
   = note: expected enum `std::option::Option<std::result::Result<std::boxed::Box<(dyn filter::Filter + 'static)>, _>>`
              found enum `std::option::Option<std::result::Result<std::boxed::Box<T>, _>>`
   = help: type parameters must be constrained to match other types
   = note: for more information, visit https://doc.rust-lang.org/book/ch10-02-traits.html#traits-as-parameters

error: aborting due to 1 previous error

For more information about this error, try `rustc --explain E0308`.
Original diagnostics will follow.

warning: `timetable-optimizer-cli` (bin "timetable-optimizer-cli" test) generated 1 warning (1 duplicate)
    Finished dev [unoptimized + debuginfo] target(s) in 3.28s

Version

rustc 1.77.1 (7cf61ebde 2024-03-27)
binary: rustc
commit-hash: 7cf61ebde7b22796c69757901dd346d0fe70bd97
commit-date: 2024-03-27
host: x86_64-unknown-linux-gnu
release: 1.77.1
LLVM version: 17.0.6

Additional Labels

@rustbot label +I-suggestion-causes-error

gepbird avatar Apr 09 '24 23:04 gepbird

Error: Label I-suggestion-causes-error can only be set by Rust team members

Please file an issue on GitHub at triagebot if there's a problem with this bot, or reach out on #t-infra on Zulip.

rustbot avatar Apr 09 '24 23:04 rustbot

@rustbot claim

J-ZhengLi avatar Apr 24 '24 02:04 J-ZhengLi