]> git.lizzy.rs Git - rust.git/blob - src/docs/octal_escapes.txt
[Arithmetic] Consider literals
[rust.git] / src / docs / octal_escapes.txt
1 ### What it does
2 Checks for `\0` escapes in string and byte literals that look like octal
3 character escapes in C.
4
5 ### Why is this bad?
6
7 C and other languages support octal character escapes in strings, where
8 a backslash is followed by up to three octal digits. For example, `\033`
9 stands for the ASCII character 27 (ESC). Rust does not support this
10 notation, but has the escape code `\0` which stands for a null
11 byte/character, and any following digits do not form part of the escape
12 sequence. Therefore, `\033` is not a compiler error but the result may
13 be surprising.
14
15 ### Known problems
16 The actual meaning can be the intended one. `\x00` can be used in these
17 cases to be unambiguous.
18
19 The lint does not trigger for format strings in `print!()`, `write!()`
20 and friends since the string is already preprocessed when Clippy lints
21 can see it.
22
23 ### Example
24 ```
25 let one = "\033[1m Bold? \033[0m";  // \033 intended as escape
26 let two = "\033\0";                 // \033 intended as null-3-3
27 ```
28
29 Use instead:
30 ```
31 let one = "\x1b[1mWill this be bold?\x1b[0m";
32 let two = "\x0033\x00";
33 ```