Skip to content

Compiler could be more helpful #116738

@3tilley

Description

@3tilley

Code

Cargo.toml

[package] name = "clippy-lint-unwrap" version = "0.1.0" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] fs-err = "2.9.0" serde = { version = "1.0.189", features = ["derive"] } serde_json = "1.0.107"

main.rs

use std::fs::File; use std::path::Path; fn main() { let path = Path::new(file!()).parent().unwrap(); let f = fs_err::File::open(path); let j = serde_json::from_reader(f).unwrap(); println!("{:?}", f); } 

Current output

error[E0277]: the trait bound `std::result::Result<fs_err::File, std::io::Error>: std::io::Read` is not satisfied --> src\main.rs:8:37 | 8 | let j = serde_json::from_reader(f).unwrap(); | ----------------------- ^ the trait `std::io::Read` is not implemented for `std::result::Result<fs_err::File, std::io::Error>` | | | required by a bound introduced by this call | note: required by a bound in `serde_json::from_reader` --> xxxxxxxxxxxxxxxxxxx\serde_json-1.0.107\src\de.rs:2590:8 | 2588 | pub fn from_reader<R, T>(rdr: R) -> Result<T> | ----------- required by a bound in this function 2589 | where 2590 | R: crate::io::Read, | ^^^^^^^^^^^^^^^ required by this bound in `from_reader`

Desired output

The error occurs because I forgot to deal with the Result, but I think the compiler could suggest here that .expect could be called here as I've seen in other situations. It seems possible to suggest that if Result<T,V> fails a trait requirement but T passes then this could be an incident of Result forgetfulness.

Rationale and extra context

This would only be checked in case of failure, so I don't think it would add overhead to the compilation time?

Other cases

I checked with std::fs and fs-err just in case, the output was the same

Anything else?

No response

Metadata

Metadata

Assignees

Labels

A-diagnosticsArea: Messages for errors, warnings, and lintsA-suggestion-diagnosticsArea: Suggestions generated by the compiler applied by `cargo fix`D-newcomer-roadblockDiagnostics: Confusing error or lint; hard to understand for new users.D-terseDiagnostics: An error or lint that doesn't give enough information about the problem at hand.T-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