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

Poor error when passing "string" instead of b"string" #44307

Closed
clarfonthey opened this issue Sep 4, 2017 · 1 comment
Closed

Poor error when passing "string" instead of b"string" #44307

clarfonthey opened this issue Sep 4, 2017 · 1 comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@clarfonthey
Copy link
Contributor

Playground: https://play.rust-lang.org/?gist=935daee28b05c2afcdf61423fdfb40f8&version=stable

fn parse_bytes(s: &[u8]) {
    // do something
}

fn main() {
    parse_bytes("hello");
}

This includes the error:

error[E0308]: mismatched types
 --> src/main.rs:6:17
  |
6 |     parse_bytes("hello");
  |                 ^^^^^^^ expected slice, found str
  |
  = note: expected type `&[u8]`
             found type `&'static str`
  = help: here are some functions which might fulfill your needs:
          - .as_bytes()

Whereas it could be much better suggesting the user to type b"hello" instead of supplying "hello".as_bytes().

@mcarton
Copy link
Member

mcarton commented Sep 4, 2017

Clippy has a lint for this exact problem.

Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Sep 6, 2017
Suggest changing literals instead of calling methods (fixes rust-lang#44307)
Mark-Simulacrum added a commit to Mark-Simulacrum/rust that referenced this issue Sep 7, 2017
Suggest changing literals instead of calling methods (fixes rust-lang#44307)
@aidanhs aidanhs added A-diagnostics Area: Messages for errors, warnings, and lints A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Sep 7, 2017
@bors bors closed this as completed in ddd0145 Sep 7, 2017
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 A-type-system Area: Type system C-enhancement Category: An issue proposing an enhancement or a PR with one. 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

3 participants