Skip to content

Commit bd121ef

Browse files
committed
Fix [non_ascii_literal] in tests
1 parent dfa780e commit bd121ef

File tree

4 files changed

+110
-39
lines changed

4 files changed

+110
-39
lines changed

clippy_lints/src/unicode.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use clippy_utils::diagnostics::span_lint_and_sugg;
22
use clippy_utils::is_lint_allowed;
3+
use clippy_utils::macros::span_is_local;
34
use clippy_utils::source::snippet;
45
use rustc_ast::ast::LitKind;
56
use rustc_errors::Applicability;
@@ -98,6 +99,10 @@ fn escape<T: Iterator<Item = char>>(s: T) -> String {
9899
}
99100

100101
fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
102+
if !span_is_local(span) {
103+
return;
104+
}
105+
101106
let string = snippet(cx, span, "");
102107
if string.chars().any(|c| ['\u{200B}', '\u{ad}', '\u{2060}'].contains(&c)) {
103108
span_lint_and_sugg(
@@ -113,6 +118,7 @@ fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
113118
Applicability::MachineApplicable,
114119
);
115120
}
121+
116122
if string.chars().any(|c| c as u32 > 0x7F) {
117123
span_lint_and_sugg(
118124
cx,
@@ -128,6 +134,7 @@ fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
128134
Applicability::MachineApplicable,
129135
);
130136
}
137+
131138
if is_lint_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
132139
span_lint_and_sugg(
133140
cx,

tests/ui/unicode.fixed

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// run-rustfix
2+
// compile-flags: --test
3+
#![allow(dead_code)]
4+
25
#[warn(clippy::invisible_characters)]
36
fn zero() {
47
print!("Here >\u{200B}< is a ZWS, and \u{200B}another");
@@ -15,22 +18,43 @@ fn canon() {
1518
print!("a\u{0300}h?"); // also ok
1619
}
1720

18-
#[warn(clippy::non_ascii_literal)]
19-
fn uni() {
20-
print!("\u{dc}ben!");
21-
print!("\u{DC}ben!"); // this is ok
22-
}
21+
mod non_ascii_literal {
22+
#![deny(clippy::non_ascii_literal)]
23+
24+
fn uni() {
25+
print!("\u{dc}ben!");
26+
print!("\u{DC}ben!"); // this is ok
27+
}
28+
29+
// issue 8013
30+
fn single_quote() {
31+
const _EMPTY_BLOCK: char = '\u{25b1}';
32+
const _FULL_BLOCK: char = '\u{25b0}';
33+
}
34+
35+
#[test]
36+
pub fn issue_7739() {
37+
// Ryū crate: https://github.com/dtolnay/ryu
38+
}
39+
40+
mod issue_8263 {
41+
#![deny(clippy::non_ascii_literal)]
42+
43+
// Re-allow for a single test
44+
#[test]
45+
#[allow(clippy::non_ascii_literal)]
46+
fn allowed() {
47+
let _ = "悲しいかな、ここに日本語を書くことはできない。";
48+
}
2349

24-
// issue 8013
25-
#[warn(clippy::non_ascii_literal)]
26-
fn single_quote() {
27-
const _EMPTY_BLOCK: char = '\u{25b1}';
28-
const _FULL_BLOCK: char = '\u{25b0}';
50+
#[test]
51+
fn denied() {
52+
let _ = "\u{60b2}\u{3057}\u{3044}\u{304b}\u{306a}\u{3001}\u{3053}\u{3053}\u{306b}\u{65e5}\u{672c}\u{8a9e}\u{3092}\u{66f8}\u{304f}\u{3053}\u{3068}\u{306f}\u{3067}\u{304d}\u{306a}\u{3044}\u{3002}";
53+
}
54+
}
2955
}
3056

3157
fn main() {
3258
zero();
33-
uni();
3459
canon();
35-
single_quote();
3660
}

tests/ui/unicode.rs

Lines changed: 36 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
// run-rustfix
2+
// compile-flags: --test
3+
#![allow(dead_code)]
4+
25
#[warn(clippy::invisible_characters)]
36
fn zero() {
47
print!("Here >​< is a ZWS, and ​another");
@@ -15,22 +18,43 @@ fn canon() {
1518
print!("a\u{0300}h?"); // also ok
1619
}
1720

18-
#[warn(clippy::non_ascii_literal)]
19-
fn uni() {
20-
print!("Üben!");
21-
print!("\u{DC}ben!"); // this is ok
22-
}
21+
mod non_ascii_literal {
22+
#![deny(clippy::non_ascii_literal)]
23+
24+
fn uni() {
25+
print!("Üben!");
26+
print!("\u{DC}ben!"); // this is ok
27+
}
28+
29+
// issue 8013
30+
fn single_quote() {
31+
const _EMPTY_BLOCK: char = '▱';
32+
const _FULL_BLOCK: char = '▰';
33+
}
34+
35+
#[test]
36+
pub fn issue_7739() {
37+
// Ryū crate: https://github.com/dtolnay/ryu
38+
}
39+
40+
mod issue_8263 {
41+
#![deny(clippy::non_ascii_literal)]
42+
43+
// Re-allow for a single test
44+
#[test]
45+
#[allow(clippy::non_ascii_literal)]
46+
fn allowed() {
47+
let _ = "悲しいかな、ここに日本語を書くことはできない。";
48+
}
2349

24-
// issue 8013
25-
#[warn(clippy::non_ascii_literal)]
26-
fn single_quote() {
27-
const _EMPTY_BLOCK: char = '▱';
28-
const _FULL_BLOCK: char = '▰';
50+
#[test]
51+
fn denied() {
52+
let _ = "悲しいかな、ここに日本語を書くことはできない。";
53+
}
54+
}
2955
}
3056

3157
fn main() {
3258
zero();
33-
uni();
3459
canon();
35-
single_quote();
3660
}

tests/ui/unicode.stderr

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,66 @@
11
error: invisible character detected
2-
--> $DIR/unicode.rs:4:12
2+
--> $DIR/unicode.rs:7:12
33
|
44
LL | print!("Here >​< is a ZWS, and ​another");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{200B}< is a ZWS, and /u{200B}another"`
66
|
77
= note: `-D clippy::invisible-characters` implied by `-D warnings`
88

99
error: invisible character detected
10-
--> $DIR/unicode.rs:6:12
10+
--> $DIR/unicode.rs:9:12
1111
|
1212
LL | print!("Here >­< is a SHY, and ­another");
1313
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{AD}< is a SHY, and /u{AD}another"`
1414

1515
error: invisible character detected
16-
--> $DIR/unicode.rs:8:12
16+
--> $DIR/unicode.rs:11:12
1717
|
1818
LL | print!("Here >⁠< is a WJ, and ⁠another");
1919
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{2060}< is a WJ, and /u{2060}another"`
2020

2121
error: non-NFC Unicode sequence detected
22-
--> $DIR/unicode.rs:14:12
22+
--> $DIR/unicode.rs:17:12
2323
|
2424
LL | print!("̀àh?");
2525
| ^^^^^ help: consider replacing the string with: `"̀àh?"`
2626
|
2727
= note: `-D clippy::unicode-not-nfc` implied by `-D warnings`
2828

2929
error: literal non-ASCII character detected
30-
--> $DIR/unicode.rs:20:12
30+
--> $DIR/unicode.rs:25:16
3131
|
32-
LL | print!("Üben!");
33-
| ^^^^^^^ help: consider replacing the string with: `"/u{dc}ben!"`
32+
LL | print!("Üben!");
33+
| ^^^^^^^ help: consider replacing the string with: `"/u{dc}ben!"`
3434
|
35-
= note: `-D clippy::non-ascii-literal` implied by `-D warnings`
35+
note: the lint level is defined here
36+
--> $DIR/unicode.rs:22:13
37+
|
38+
LL | #![deny(clippy::non_ascii_literal)]
39+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
40+
41+
error: literal non-ASCII character detected
42+
--> $DIR/unicode.rs:31:36
43+
|
44+
LL | const _EMPTY_BLOCK: char = '▱';
45+
| ^^^ help: consider replacing the string with: `'/u{25b1}'`
3646

3747
error: literal non-ASCII character detected
38-
--> $DIR/unicode.rs:27:32
48+
--> $DIR/unicode.rs:32:35
3949
|
40-
LL | const _EMPTY_BLOCK: char = '';
41-
| ^^^ help: consider replacing the string with: `'/u{25b1}'`
50+
LL | const _FULL_BLOCK: char = '';
51+
| ^^^ help: consider replacing the string with: `'/u{25b0}'`
4252

4353
error: literal non-ASCII character detected
44-
--> $DIR/unicode.rs:28:31
54+
--> $DIR/unicode.rs:52:21
55+
|
56+
LL | let _ = "悲しいかな、ここに日本語を書くことはできない。";
57+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"/u{60b2}/u{3057}/u{3044}/u{304b}/u{306a}/u{3001}/u{3053}/u{3053}/u{306b}/u{65e5}/u{672c}/u{8a9e}/u{3092}/u{66f8}/u{304f}/u{3053}/u{3068}/u{306f}/u{3067}/u{304d}/u{306a}/u{3044}/u{3002}"`
58+
|
59+
note: the lint level is defined here
60+
--> $DIR/unicode.rs:41:17
4561
|
46-
LL | const _FULL_BLOCK: char = '▰';
47-
| ^^^ help: consider replacing the string with: `'/u{25b0}'`
62+
LL | #![deny(clippy::non_ascii_literal)]
63+
| ^^^^^^^^^^^^^^^^^^^^^^^^^
4864

49-
error: aborting due to 7 previous errors
65+
error: aborting due to 8 previous errors
5066

0 commit comments

Comments
 (0)