Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

Compiler could be more helpful #116738

Open
3tilley opened this issue Oct 14, 2023 · 4 comments
Open

Compiler could be more helpful #116738

3tilley opened this issue Oct 14, 2023 · 4 comments
Assignees
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@3tilley
Copy link
Contributor

3tilley commented Oct 14, 2023

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

@3tilley 3tilley added A-diagnostics Area: Messages for errors, warnings, and lints T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Oct 14, 2023
@rustbot rustbot added the needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. label Oct 14, 2023
@fmease fmease added D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. and removed needs-triage This issue may need triage. Remove it if it has been sufficiently triaged. labels Oct 16, 2023
@chenyukang
Copy link
Member

For anyone may want to resolve this issue, I made a sample without the crate:

use std::path::Path;

struct File {
    path: String,
}

impl File {
    fn new(path: &Path) -> Self {
        Self { path: path.to_str().unwrap().to_string() }
    }
}

fn open(path: &Path) -> Option<File> {
    Some(File::new(&path))
}

trait FileAPI {
    fn read(self) -> String;
}

impl FileAPI for File {
    fn read(self) -> String {
        std::fs::read_to_string(&self.path).unwrap()
    }
}

fn test_file<T>(f: T) -> String
where
    T: FileAPI,
{
    f.read()
}

fn main() {
    let path = Path::new(file!()).parent().unwrap();
    let file = open(path);
    let _ = test_file(file);
}

matthiaskrgr added a commit to matthiaskrgr/rust that referenced this issue Oct 24, 2023
…xpect, r=b-naber

Suggest unwrap/expect for let binding type mismatch

Found it when investigating rust-lang#116738
I'm not sure whether it's a good style to suggest `unwrap`, seems it's may helpful for newcomers.

rust-lang#116738 needs another fix to improve it.
rust-timer added a commit to rust-lang-ci/rust that referenced this issue Oct 24, 2023
Rollup merge of rust-lang#116841 - chenyukang:yukang-suggest-unwrap-expect, r=b-naber

Suggest unwrap/expect for let binding type mismatch

Found it when investigating rust-lang#116738
I'm not sure whether it's a good style to suggest `unwrap`, seems it's may helpful for newcomers.

rust-lang#116738 needs another fix to improve it.
@3tilley
Copy link
Contributor Author

3tilley commented Oct 31, 2023

@chenyukang are you working on this or can I claim?

@chenyukang
Copy link
Member

@3tilley you can work on it, please go ahead.

@3tilley
Copy link
Contributor Author

3tilley commented Oct 31, 2023

@rustbot claim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints D-newcomer-roadblock Diagnostics: Confusing error or lint; hard to understand for new users. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants