Add new lint for byte char slices
This patch adds a new lint that checks for potentially harder to read byte char slices: &[b'a', b'b'] and suggests to replace them with the easier to read b"ab" form.
Fixes #10147
changelog: new lint: [byte_char_slice]
#10155
Thanks for the pull request, and welcome! The Rust team is excited to review your changes, and you should hear from @Manishearth (or someone else) soon.
Please see the contribution instructions for more information.
Potentially also @Alexendoo ? (Since you 'approved' the idea)
---- src/byte_char_slice.rs - byte_char_slice::BYTE_CHAR_SLICE (line 21) stdout ----
error[E0308]: mismatched types
--> src/byte_char_slice.rs:22:1
|
2 | fn main() { #[allow(non_snake_case)] fn _doctest_main_src_byte_char_slice_rs_21_0() {
| - help: try adding a return type: `-> &'static [u8; 5]`
3 | b"Hello"
| ^^^^^^^^ expected `()`, found `&[u8; 5]`
error: aborting due to previous error
For more information about this error, try `rustc --explain E0308`.
Couldn't compile the test.
---- src/byte_char_slice.rs - byte_char_slice::BYTE_CHAR_SLICE (line 17) stdout ----
error[E0308]: mismatched types
--> src/byte_char_slice.rs:18:1
|
2 | fn main() { #[allow(non_snake_case)] fn _doctest_main_src_byte_char_slice_rs_17_0() {
| - help: try adding a return type: `-> &[u8; 5]`
3 | &[b'H', b'e', b'l', b'l', b'o']
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ expected `()`, found `&[u8; 5]`
error: aborting due to previous error
:umbrella: The latest upstream changes (presumably #10206) made this pull request unmergeable. Please resolve the merge conflicts.
@TheNeikos I think we can merge this after a rebase.
Hey @TheNeikos this is a ping from triage. Would you mind rebasing this PR?
@rustbot author
I've just rebased, since that seamed to be the last thing holding this back. I'll create an FCP for this.
:umbrella: The latest upstream changes (presumably #12873) made this pull request unmergeable. Please resolve the merge conflicts.
I'd say the FCP is done. The only feedback was to rename the lint, which I've done.
@Manishearth Can we r+ it, or do you want to check something else?
FCP: https://rust-lang.zulipchat.com/#narrow/stream/257328-clippy/topic/FCP.3A.20.60byte_char_slice.60.20rust-clippy.2310155/near/445939887
Yeah that sounds good!
@bors r+
:pushpin: Commit 88c4a224807144b535854b53fbd46e6dc18555a1 has been approved by Manishearth
It is now in the queue for this repository.
:hourglass: Testing commit 88c4a224807144b535854b53fbd46e6dc18555a1 with merge 2b01d6921290b0d5fdc3a781b0e5c15a44cadd15...
:sunny: Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test Approved by: Manishearth Pushing 2b01d6921290b0d5fdc3a781b0e5c15a44cadd15 to master...
I think this lint has a false positive
error: can be more succinctly written as a byte str
--> src/symbolize/perf_map.rs:381:58
|
381 | let () = child.stdin.as_ref().unwrap().write_all(&[b'\n']).unwrap();
| ^^^^^^^^ help: try: `b"\n"`
|
= help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#byte_char_slices
= note: `-D clippy::byte-char-slices` implied by `-D warnings`
= help: to override `-D warnings` add `#[allow(clippy::byte_char_slices)]`
The suggestion seems blatantly wrong, as the code doesn't even compile if I follow it:
error[E0308]: mismatched types
--> src/symbolize/perf_map.rs:382:60
|
382 | let () = child.stdin.as_ref().unwrap().write_all(&[b"\n"]).unwrap();
| ^^^^^ expected `u8`, found `&[u8; 1]`
@danielocfb The arrows in the diagnostic cover the whole function argument, so clippy is suggesting .write_all(b"\n"), which should compile.
Ah my bad. Sorry for the noise then.