Skip to content

Commit dce670c

Browse files
authored
Rollup merge of #4119 - BO41:non_ascii_literal, r=flip1995
Improve non ascii literal This PR improves the example of the [non_ascii_literal](https://rust-lang.github.io/rust-clippy/master/index.html#non_ascii_literal) lint. It also makes it auto-fixable. Please review. This is my first PR to this project. (Thanks @flip1995 for the help :) changelog: none fixes #4117
2 parents f0a7673 + 36c8aab commit dce670c

File tree

2 files changed

+23
-27
lines changed

2 files changed

+23
-27
lines changed

clippy_lints/src/unicode.rs

+23-21
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
use crate::utils::{is_allowed, snippet, span_help_and_lint};
1+
use crate::utils::{is_allowed, snippet, span_lint_and_sugg};
22
use rustc::hir::*;
33
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
44
use rustc::{declare_lint_pass, declare_tool_lint};
5+
use rustc_errors::Applicability;
56
use syntax::ast::LitKind;
67
use syntax::source_map::Span;
78
use unicode_normalization::UnicodeNormalization;
@@ -34,7 +35,11 @@ declare_clippy_lint! {
3435
///
3536
/// **Example:**
3637
/// ```rust
37-
/// let x = "Hä?"
38+
/// let x = String::from("€");
39+
/// ```
40+
/// Could be written as:
41+
/// ```rust
42+
/// let x = String::from("\u{20ac}");
3843
/// ```
3944
pub NON_ASCII_LITERAL,
4045
pedantic,
@@ -87,43 +92,40 @@ fn escape<T: Iterator<Item = char>>(s: T) -> String {
8792
fn check_str(cx: &LateContext<'_, '_>, span: Span, id: HirId) {
8893
let string = snippet(cx, span, "");
8994
if string.contains('\u{200B}') {
90-
span_help_and_lint(
95+
span_lint_and_sugg(
9196
cx,
9297
ZERO_WIDTH_SPACE,
9398
span,
9499
"zero-width space detected",
95-
&format!(
96-
"Consider replacing the string with:\n\"{}\"",
97-
string.replace("\u{200B}", "\\u{200B}")
98-
),
100+
"consider replacing the string with",
101+
string.replace("\u{200B}", "\\u{200B}"),
102+
Applicability::MachineApplicable,
99103
);
100104
}
101105
if string.chars().any(|c| c as u32 > 0x7F) {
102-
span_help_and_lint(
106+
span_lint_and_sugg(
103107
cx,
104108
NON_ASCII_LITERAL,
105109
span,
106110
"literal non-ASCII character detected",
107-
&format!(
108-
"Consider replacing the string with:\n\"{}\"",
109-
if is_allowed(cx, UNICODE_NOT_NFC, id) {
110-
escape(string.chars())
111-
} else {
112-
escape(string.nfc())
113-
}
114-
),
111+
"consider replacing the string with",
112+
if is_allowed(cx, UNICODE_NOT_NFC, id) {
113+
escape(string.chars())
114+
} else {
115+
escape(string.nfc())
116+
},
117+
Applicability::MachineApplicable,
115118
);
116119
}
117120
if is_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
118-
span_help_and_lint(
121+
span_lint_and_sugg(
119122
cx,
120123
UNICODE_NOT_NFC,
121124
span,
122125
"non-nfc unicode sequence detected",
123-
&format!(
124-
"Consider replacing the string with:\n\"{}\"",
125-
string.nfc().collect::<String>()
126-
),
126+
"consider replacing the string with",
127+
string.nfc().collect::<String>(),
128+
Applicability::MachineApplicable,
127129
);
128130
}
129131
}

tests/ui/unicode.stderr

-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ LL | print!("Here >​< is a ZWS, and ​another");
55
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
66
|
77
= note: `-D clippy::zero-width-space` implied by `-D warnings`
8-
= help: Consider replacing the string with:
9-
""Here >/u{200B}< is a ZWS, and /u{200B}another""
108

119
error: non-nfc unicode sequence detected
1210
--> $DIR/unicode.rs:9:12
@@ -15,8 +13,6 @@ LL | print!("̀àh?");
1513
| ^^^^^
1614
|
1715
= note: `-D clippy::unicode-not-nfc` implied by `-D warnings`
18-
= help: Consider replacing the string with:
19-
""̀àh?""
2016

2117
error: literal non-ASCII character detected
2218
--> $DIR/unicode.rs:15:12
@@ -25,8 +21,6 @@ LL | print!("Üben!");
2521
| ^^^^^^^
2622
|
2723
= note: `-D clippy::non-ascii-literal` implied by `-D warnings`
28-
= help: Consider replacing the string with:
29-
""/u{dc}ben!""
3024

3125
error: aborting due to 3 previous errors
3226

0 commit comments

Comments
 (0)