rust-clippy icon indicating copy to clipboard operation
rust-clippy copied to clipboard

Add new lint for byte char slices

Open TheNeikos opened this issue 3 years ago • 7 comments

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

TheNeikos avatar Jan 04 '23 08:01 TheNeikos

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.

rustbot avatar Jan 04 '23 08:01 rustbot

Potentially also @Alexendoo ? (Since you 'approved' the idea)

TheNeikos avatar Jan 04 '23 08:01 TheNeikos

---- 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

Manishearth avatar Jan 05 '23 19:01 Manishearth

:umbrella: The latest upstream changes (presumably #10206) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Jan 19 '23 15:01 bors

@TheNeikos I think we can merge this after a rebase.

llogiq avatar Mar 24 '23 22:03 llogiq

Hey @TheNeikos this is a ping from triage. Would you mind rebasing this PR?

@rustbot author

xFrednet avatar Mar 21 '24 15:03 xFrednet

I've just rebased, since that seamed to be the last thing holding this back. I'll create an FCP for this.

xFrednet avatar Jun 20 '24 19:06 xFrednet

:umbrella: The latest upstream changes (presumably #12873) made this pull request unmergeable. Please resolve the merge conflicts.

bors avatar Jul 04 '24 05:07 bors

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

xFrednet avatar Jul 04 '24 12:07 xFrednet

Yeah that sounds good!

@bors r+

Manishearth avatar Jul 04 '24 16:07 Manishearth

:pushpin: Commit 88c4a224807144b535854b53fbd46e6dc18555a1 has been approved by Manishearth

It is now in the queue for this repository.

bors avatar Jul 04 '24 16:07 bors

:hourglass: Testing commit 88c4a224807144b535854b53fbd46e6dc18555a1 with merge 2b01d6921290b0d5fdc3a781b0e5c15a44cadd15...

bors avatar Jul 04 '24 16:07 bors

:sunny: Test successful - checks-action_dev_test, checks-action_remark_test, checks-action_test Approved by: Manishearth Pushing 2b01d6921290b0d5fdc3a781b0e5c15a44cadd15 to master...

bors avatar Jul 04 '24 16:07 bors

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 avatar Sep 05 '24 16:09 danielocfb

@danielocfb The arrows in the diagnostic cover the whole function argument, so clippy is suggesting .write_all(b"\n"), which should compile.

y21 avatar Sep 05 '24 16:09 y21

Ah my bad. Sorry for the noise then.

danielocfb avatar Sep 05 '24 16:09 danielocfb