rust-lang / rust-lang/rust-clippy

Clippy fix to owned Result returned on if .is_err(), produces broken code

Open
#10,246 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

C-bug I-suggestion-causes-error
Dominant language
Rust
Stars
13.5k
Forks
2.2k
Avg merge
2d 10h
Merged PRs (30d)
32

Description

Summary

This is the clippy warning for the below code. Following it produces broken code.

warning: this block may be rewritten with the `?` operator
  --> src/lib.rs:19:9
   |
19 | /         if res.is_err() {
20 | |             return res;
21 | |         }
   | |_________^ help: replace it with: `res.as_ref()?;`
Reproducer
pub struct Error;

pub struct First;

pub trait CheckError {
    fn check_error(&self) -> Result<(), Error>;
}

impl CheckError for First {
    fn check_error(&self) -> Result<(), Error> {
        Ok(())
    }
}

pub fn check_many_errors(item_list: &Vec<First>) -> Result<(), Error> {
    for item in item_list {
        let res = item.check_error();

        if res.is_err() {
            return res;
        }
    }

    Ok(())
}

cargo clippy --fix

produces

warning: failed to automatically apply fixes suggested by rustc to crate `testy`

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

  * src/lib.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/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[E0277]: `?` couldn't convert the error to `Error`
  --> src/lib.rs:19:21
   |
15 | pub fn check_many_errors(item_list: &Vec<First>) -> Result<(), Error> {
   |                                                     ----------------- expected `Error` because of this
...
19 |         res.as_ref()?;
   |                     ^ the trait `std::convert::From<&Error>` is not implemented for `Error`
   |
   = note: the question mark operation (`?`) implicitly performs a conversion on the error value using the `From` trait
   = help: the following other types implement trait `std::ops::FromResidual<R>`:
             <std::result::Result<T, F> as std::ops::FromResidual<std::ops::Yeet<E>>>
             <std::result::Result<T, F> as std::ops::FromResidual<std::result::Result<std::convert::Infallible, E>>>
   = note: required for `std::result::Result<(), Error>` to implement `std::ops::FromResidual<std::result::Result<std::convert::Infallible, &Error>>`

error: aborting due to previous error

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



Version
rustc 1.69.0-nightly (ef982929c 2023-01-27)
binary: rustc
commit-hash: ef982929c0b653436a6ea6892a2a839fba7c8b57
commit-date: 2023-01-27
host: x86_64-unknown-linux-gnu
release: 1.69.0-nightly
LLVM version: 15.0.7
Additional Labels

@rustbot label +I-suggestion-causes-error

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the reproducer in src/lib.rs and run cargo clippy --fix using the reported rustc version. Trace the lint that suggests res.as_ref()? and add a regression case covering an owned Result whose error cannot be borrowed through ?. Done means the suggested fix no longer produces broken code and the reproducer compiles.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.