Skip to content

Commit

Permalink
Rollup merge of rust-lang#4119 - BO41:non_ascii_literal, r=flip1995
Browse files Browse the repository at this point in the history
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 rust-lang#4117
  • Loading branch information
flip1995 authored May 27, 2019
2 parents f0a7673 + 36c8aab commit dce670c
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 27 deletions.
44 changes: 23 additions & 21 deletions clippy_lints/src/unicode.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::utils::{is_allowed, snippet, span_help_and_lint};
use crate::utils::{is_allowed, snippet, span_lint_and_sugg};
use rustc::hir::*;
use rustc::lint::{LateContext, LateLintPass, LintArray, LintPass};
use rustc::{declare_lint_pass, declare_tool_lint};
use rustc_errors::Applicability;
use syntax::ast::LitKind;
use syntax::source_map::Span;
use unicode_normalization::UnicodeNormalization;
Expand Down Expand Up @@ -34,7 +35,11 @@ declare_clippy_lint! {
///
/// **Example:**
/// ```rust
/// let x = "Hä?"
/// let x = String::from("€");
/// ```
/// Could be written as:
/// ```rust
/// let x = String::from("\u{20ac}");
/// ```
pub NON_ASCII_LITERAL,
pedantic,
Expand Down Expand Up @@ -87,43 +92,40 @@ fn escape<T: Iterator<Item = char>>(s: T) -> String {
fn check_str(cx: &LateContext<'_, '_>, span: Span, id: HirId) {
let string = snippet(cx, span, "");
if string.contains('\u{200B}') {
span_help_and_lint(
span_lint_and_sugg(
cx,
ZERO_WIDTH_SPACE,
span,
"zero-width space detected",
&format!(
"Consider replacing the string with:\n\"{}\"",
string.replace("\u{200B}", "\\u{200B}")
),
"consider replacing the string with",
string.replace("\u{200B}", "\\u{200B}"),
Applicability::MachineApplicable,
);
}
if string.chars().any(|c| c as u32 > 0x7F) {
span_help_and_lint(
span_lint_and_sugg(
cx,
NON_ASCII_LITERAL,
span,
"literal non-ASCII character detected",
&format!(
"Consider replacing the string with:\n\"{}\"",
if is_allowed(cx, UNICODE_NOT_NFC, id) {
escape(string.chars())
} else {
escape(string.nfc())
}
),
"consider replacing the string with",
if is_allowed(cx, UNICODE_NOT_NFC, id) {
escape(string.chars())
} else {
escape(string.nfc())
},
Applicability::MachineApplicable,
);
}
if is_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
span_help_and_lint(
span_lint_and_sugg(
cx,
UNICODE_NOT_NFC,
span,
"non-nfc unicode sequence detected",
&format!(
"Consider replacing the string with:\n\"{}\"",
string.nfc().collect::<String>()
),
"consider replacing the string with",
string.nfc().collect::<String>(),
Applicability::MachineApplicable,
);
}
}
6 changes: 0 additions & 6 deletions tests/ui/unicode.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ LL | print!("Here >​< is a ZWS, and ​another");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
|
= note: `-D clippy::zero-width-space` implied by `-D warnings`
= help: Consider replacing the string with:
""Here >/u{200B}< is a ZWS, and /u{200B}another""

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

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

error: aborting due to 3 previous errors

0 comments on commit dce670c

Please sign in to comment.