about summary refs log tree commit diff
path: root/src/tools/clippy/tests/ui/sliced_string_as_bytes.rs
blob: 67985ae5b9842b14b31ef09e18a90130ad1044c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#![allow(unused)]
#![warn(clippy::sliced_string_as_bytes)]

use std::ops::{Index, Range};

struct Foo;

struct Bar;

impl Bar {
    fn as_bytes(&self) -> &[u8] {
        &[0, 1, 2, 3]
    }
}

impl Index<Range<usize>> for Foo {
    type Output = Bar;

    fn index(&self, _: Range<usize>) -> &Self::Output {
        &Bar
    }
}

fn main() {
    let s = "Lorem ipsum";
    let string: String = "dolor sit amet".to_owned();

    let bytes = s[1..5].as_bytes();
    //~^ sliced_string_as_bytes
    let bytes = string[1..].as_bytes();
    //~^ sliced_string_as_bytes
    let bytes = "consectetur adipiscing"[..=5].as_bytes();
    //~^ sliced_string_as_bytes

    let f = Foo;
    let bytes = f[0..4].as_bytes();
}