Skip to content

Commit 515ca93

Browse files
committed
Look for soft hyphens as well
1 parent f0eac45 commit 515ca93

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

clippy_lints/src/unicode.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,18 @@ use rustc_span::source_map::Span;
88
use unicode_normalization::UnicodeNormalization;
99

1010
declare_clippy_lint! {
11-
/// **What it does:** Checks for the Unicode zero-width space in the code.
11+
/// **What it does:** Checks for invisible Unicode characters in the code.
1212
///
1313
/// **Why is this bad?** Having an invisible character in the code makes for all
1414
/// sorts of April fools, but otherwise is very much frowned upon.
1515
///
1616
/// **Known problems:** None.
1717
///
18-
/// **Example:** You don't see it, but there may be a zero-width space
19-
/// somewhere in this text.
18+
/// **Example:** You don't see it, but there may be a zero-width space or soft hyphen
19+
/// some­where in this text.
2020
pub ZERO_WIDTH_SPACE,
2121
correctness,
22-
"using a zero-width space in a string literal, which is confusing"
22+
"using an invisible character in a string literal, which is confusing"
2323
}
2424

2525
declare_clippy_lint! {
@@ -91,14 +91,14 @@ fn escape<T: Iterator<Item = char>>(s: T) -> String {
9191

9292
fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
9393
let string = snippet(cx, span, "");
94-
if string.contains('\u{200B}') {
94+
if let Some(invisible) = string.chars().find(|c| ['\u{200B}', '\u{ad}'].contains(&c)) {
9595
span_lint_and_sugg(
9696
cx,
9797
ZERO_WIDTH_SPACE,
9898
span,
99-
"zero-width space detected",
99+
&format!("invisible character detected: {:?}", invisible),
100100
"consider replacing the string with",
101-
string.replace("\u{200B}", "\\u{200B}"),
101+
string.replace("\u{200B}", "\\u{200B}").replace("\u{ad}", "\\u{AD}"),
102102
Applicability::MachineApplicable,
103103
);
104104
}

tests/ui/unicode.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
fn zero() {
33
print!("Here >​< is a ZWS, and ​another");
44
print!("This\u{200B}is\u{200B}fine");
5+
print!("Here >­< is a SHY, and ­another");
6+
print!("This\u{ad}is\u{ad}fine");
57
}
68

79
#[warn(clippy::unicode_not_nfc)]

tests/ui/unicode.stderr

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,32 @@
1-
error: zero-width space detected
1+
error: invisible character detected: '/u{200b}'
22
--> $DIR/unicode.rs:3: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::zero-width-space` implied by `-D warnings`
88

9+
error: invisible character detected: '/u{ad}'
10+
--> $DIR/unicode.rs:5:12
11+
|
12+
LL | print!("Here >­< is a SHY, and ­another");
13+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{AD}< is a SHY, and /u{AD}another"`
14+
915
error: non-NFC Unicode sequence detected
10-
--> $DIR/unicode.rs:9:12
16+
--> $DIR/unicode.rs:11:12
1117
|
1218
LL | print!("̀àh?");
1319
| ^^^^^ help: consider replacing the string with: `"̀àh?"`
1420
|
1521
= note: `-D clippy::unicode-not-nfc` implied by `-D warnings`
1622

1723
error: literal non-ASCII character detected
18-
--> $DIR/unicode.rs:15:12
24+
--> $DIR/unicode.rs:17:12
1925
|
2026
LL | print!("Üben!");
2127
| ^^^^^^^ help: consider replacing the string with: `"/u{dc}ben!"`
2228
|
2329
= note: `-D clippy::non-ascii-literal` implied by `-D warnings`
2430

25-
error: aborting due to 3 previous errors
31+
error: aborting due to 4 previous errors
2632

0 commit comments

Comments
 (0)