Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

false positive: question_mark when returning custom Error type #7859

Closed
matthiaskrgr opened this issue Oct 22, 2021 · 2 comments · Fixed by #7860
Closed

false positive: question_mark when returning custom Error type #7859

matthiaskrgr opened this issue Oct 22, 2021 · 2 comments · Fixed by #7860
Assignees
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied

Comments

@matthiaskrgr
Copy link
Member

matthiaskrgr commented Oct 22, 2021

Lint name: question_mark

I tried this code:

    let mut cwd: PathBuf = if let Ok(cwd) = env::current_dir() {
        cwd
    } else {
        return Err(Error::NoCWD);
    };

I expected to see this happen:
There should be no warning.
Error::NoCWD is my own error type (an enum) and it does not impl std::convert::From<std::io::Error> or anything like that. So I can't use ? on it directly.

Instead, this happened:

warning: this if-let-else may be rewritten with the `?` operator
  --> src/commands/local.rs:53:28
   |
53 |       let mut cwd: PathBuf = if let Ok(cwd) = env::current_dir() {
   |  ____________________________^
54 | |         cwd
55 | |     } else {
56 | |         return Err(Error::NoCWD);
57 | |     };
   | |_____^ help: replace it with: `env::current_dir()?`
   |
note: the lint level is defined here

Meta

clippy 0.1.58 (68a698b 2021-10-21)

rustc 1.58.0-nightly (68a698baf 2021-10-21)
binary: rustc
commit-hash: 68a698baf6bfc61d85ce6e25122a092c60c7f21a
commit-date: 2021-10-21
host: x86_64-unknown-linux-gnu
release: 1.58.0-nightly
LLVM version: 13.0.0

Rust version (rustc -Vv):

rustc 1.46.0-nightly (f455e46ea 2020-06-20)
binary: rustc
commit-hash: f455e46eae1a227d735091091144601b467e1565
commit-date: 2020-06-20
host: x86_64-unknown-linux-gnu
release: 1.46.0-nightly
LLVM version: 10.0

@ rustbot label +I-suggestion-causes-error

@matthiaskrgr matthiaskrgr added C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied labels Oct 22, 2021
@matthiaskrgr
Copy link
Member Author

matthiaskrgr commented Oct 22, 2021

Code that clippy suggests:

error[E0277]: `?` couldn't convert the error to `library::Error`
   --> src/commands/local.rs:53:46
    |
51  | pub(crate) fn get_manifest() -> Result<PathBuf, Error> {
    |                                 ---------------------- expected `library::Error` because of this
52  |     // get the cwd
53  |     let mut cwd: PathBuf = env::current_dir()?;
    |                                              ^ the trait `From<std::io::Error>` is not implemented for `library::Error`
    |
    = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
    = note: required because of the requirements on the impl of `FromResidual<Result<Infallible, std::io::Error>>` for `Result<PathBuf, library::Error>`
note: required by `from_residual`
   --> /home/matthias/.rustup/toolchains/master/lib/rustlib/src/rust/library/core/src/ops/try_trait.rs:339:5
    |
339 |     fn from_residual(residual: R) -> Self;
    |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

For more information about this error, try `rustc --explain E0277`.

@dswij
Copy link
Member

dswij commented Oct 22, 2021

@rustbot claim

dswij added a commit to dswij/rust-clippy that referenced this issue Oct 22, 2021
dtolnay added a commit to dtolnay/serde-yaml that referenced this issue Oct 23, 2021
rust-lang/rust-clippy#7859

    error: this block may be rewritten with the `?` operator
       --> src/ser.rs:871:9
        |
    871 | /         if self.writer.write_all(s.as_bytes()).is_err() {
    872 | |             return Err(fmt::Error);
    873 | |         }
        | |_________^ help: replace it with: `self.writer.write_all(s.as_bytes())?;`
        |
        = note: `-D clippy::question-mark` implied by `-D clippy::all`
        = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#question_mark

Clippy's suggested replacement does not compile.

    error[E0277]: `?` couldn't convert the error to `std::fmt::Error`
       --> src/ser.rs:871:44
        |
    871 |         self.writer.write_all(s.as_bytes())?;
        |                                            ^ the trait `std::convert::From<std::io::Error>` is not implemented for `std::fmt::Error`
        |
        = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
        = note: required because of the requirements on the impl of `std::ops::FromResidual<std::result::Result<std::convert::Infallible, std::io::Error>>` for `std::result::Result<(), std::fmt::Error>`
    note: required by `std::ops::FromResidual::from_residual`
       --> nightly-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/ops/try_trait.rs:339:5
        |
    339 |     fn from_residual(residual: R) -> Self;
        |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
dswij added a commit to dswij/rust-clippy that referenced this issue Oct 26, 2021
@bors bors closed this as completed in ba2ac3e Oct 26, 2021
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
C-bug Category: Clippy is not doing the correct thing I-false-positive Issue: The lint was triggered on code it shouldn't have I-suggestion-causes-error Issue: The suggestions provided by this Lint cause an ICE/error when applied
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants