|
1 |
| -use crate::utils::{is_allowed, snippet, span_help_and_lint}; |
| 1 | +use crate::utils::{is_allowed, snippet, span_lint_and_sugg}; |
2 | 2 | use rustc::hir::*;
|
3 | 3 | use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
|
4 | 4 | use rustc::{declare_lint_pass, declare_tool_lint};
|
| 5 | +use rustc_errors::Applicability; |
5 | 6 | use syntax::ast::LitKind;
|
6 | 7 | use syntax::source_map::Span;
|
7 | 8 | use unicode_normalization::UnicodeNormalization;
|
@@ -34,7 +35,11 @@ declare_clippy_lint! {
|
34 | 35 | ///
|
35 | 36 | /// **Example:**
|
36 | 37 | /// ```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}"); |
38 | 43 | /// ```
|
39 | 44 | pub NON_ASCII_LITERAL,
|
40 | 45 | pedantic,
|
@@ -87,43 +92,40 @@ fn escape<T: Iterator<Item = char>>(s: T) -> String {
|
87 | 92 | fn check_str(cx: &LateContext<'_, '_>, span: Span, id: HirId) {
|
88 | 93 | let string = snippet(cx, span, "");
|
89 | 94 | if string.contains('\u{200B}') {
|
90 |
| - span_help_and_lint( |
| 95 | + span_lint_and_sugg( |
91 | 96 | cx,
|
92 | 97 | ZERO_WIDTH_SPACE,
|
93 | 98 | span,
|
94 | 99 | "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, |
99 | 103 | );
|
100 | 104 | }
|
101 | 105 | if string.chars().any(|c| c as u32 > 0x7F) {
|
102 |
| - span_help_and_lint( |
| 106 | + span_lint_and_sugg( |
103 | 107 | cx,
|
104 | 108 | NON_ASCII_LITERAL,
|
105 | 109 | span,
|
106 | 110 | "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, |
115 | 118 | );
|
116 | 119 | }
|
117 | 120 | 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( |
119 | 122 | cx,
|
120 | 123 | UNICODE_NOT_NFC,
|
121 | 124 | span,
|
122 | 125 | "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, |
127 | 129 | );
|
128 | 130 | }
|
129 | 131 | }
|
0 commit comments