Skip to content

Commit

Permalink
Auto merge of rust-lang#9327 - Serial-ATA:non_ascii_literal_macro, r=…
Browse files Browse the repository at this point in the history
…Alexendoo

Fix [`non_ascii_literal`] in tests

changelog: Don't lint [`non_ascii_literal`] when using non-ascii comments in tests
changelog: Don't lint [`non_ascii_literal`] when `allow`ed on tests

closes: rust-lang#7739
closes: rust-lang#8263
  • Loading branch information
bors committed Aug 16, 2022
2 parents 048e4d0 + bd121ef commit f4f5bb4
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 39 deletions.
7 changes: 7 additions & 0 deletions clippy_lints/src/unicode.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_lint_allowed;
use clippy_utils::macros::span_is_local;
use clippy_utils::source::snippet;
use rustc_ast::ast::LitKind;
use rustc_errors::Applicability;
Expand Down Expand Up @@ -98,6 +99,10 @@ fn escape<T: Iterator<Item = char>>(s: T) -> String {
}

fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
if !span_is_local(span) {
return;
}

let string = snippet(cx, span, "");
if string.chars().any(|c| ['\u{200B}', '\u{ad}', '\u{2060}'].contains(&c)) {
span_lint_and_sugg(
Expand All @@ -113,6 +118,7 @@ fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
Applicability::MachineApplicable,
);
}

if string.chars().any(|c| c as u32 > 0x7F) {
span_lint_and_sugg(
cx,
Expand All @@ -128,6 +134,7 @@ fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
Applicability::MachineApplicable,
);
}

if is_lint_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
span_lint_and_sugg(
cx,
Expand Down
48 changes: 36 additions & 12 deletions tests/ui/unicode.fixed
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// run-rustfix
// compile-flags: --test
#![allow(dead_code)]

#[warn(clippy::invisible_characters)]
fn zero() {
print!("Here >\u{200B}< is a ZWS, and \u{200B}another");
Expand All @@ -15,22 +18,43 @@ fn canon() {
print!("a\u{0300}h?"); // also ok
}

#[warn(clippy::non_ascii_literal)]
fn uni() {
print!("\u{dc}ben!");
print!("\u{DC}ben!"); // this is ok
}
mod non_ascii_literal {
#![deny(clippy::non_ascii_literal)]

fn uni() {
print!("\u{dc}ben!");
print!("\u{DC}ben!"); // this is ok
}

// issue 8013
fn single_quote() {
const _EMPTY_BLOCK: char = '\u{25b1}';
const _FULL_BLOCK: char = '\u{25b0}';
}

#[test]
pub fn issue_7739() {
// Ryū crate: https://github.com/dtolnay/ryu
}

mod issue_8263 {
#![deny(clippy::non_ascii_literal)]

// Re-allow for a single test
#[test]
#[allow(clippy::non_ascii_literal)]
fn allowed() {
let _ = "悲しいかな、ここに日本語を書くことはできない。";
}

// issue 8013
#[warn(clippy::non_ascii_literal)]
fn single_quote() {
const _EMPTY_BLOCK: char = '\u{25b1}';
const _FULL_BLOCK: char = '\u{25b0}';
#[test]
fn denied() {
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}";
}
}
}

fn main() {
zero();
uni();
canon();
single_quote();
}
48 changes: 36 additions & 12 deletions tests/ui/unicode.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
// run-rustfix
// compile-flags: --test
#![allow(dead_code)]

#[warn(clippy::invisible_characters)]
fn zero() {
print!("Here >​< is a ZWS, and ​another");
Expand All @@ -15,22 +18,43 @@ fn canon() {
print!("a\u{0300}h?"); // also ok
}

#[warn(clippy::non_ascii_literal)]
fn uni() {
print!("Üben!");
print!("\u{DC}ben!"); // this is ok
}
mod non_ascii_literal {
#![deny(clippy::non_ascii_literal)]

fn uni() {
print!("Üben!");
print!("\u{DC}ben!"); // this is ok
}

// issue 8013
fn single_quote() {
const _EMPTY_BLOCK: char = '▱';
const _FULL_BLOCK: char = '▰';
}

#[test]
pub fn issue_7739() {
// Ryū crate: https://github.com/dtolnay/ryu
}

mod issue_8263 {
#![deny(clippy::non_ascii_literal)]

// Re-allow for a single test
#[test]
#[allow(clippy::non_ascii_literal)]
fn allowed() {
let _ = "悲しいかな、ここに日本語を書くことはできない。";
}

// issue 8013
#[warn(clippy::non_ascii_literal)]
fn single_quote() {
const _EMPTY_BLOCK: char = '▱';
const _FULL_BLOCK: char = '▰';
#[test]
fn denied() {
let _ = "悲しいかな、ここに日本語を書くことはできない。";
}
}
}

fn main() {
zero();
uni();
canon();
single_quote();
}
46 changes: 31 additions & 15 deletions tests/ui/unicode.stderr
Original file line number Diff line number Diff line change
@@ -1,50 +1,66 @@
error: invisible character detected
--> $DIR/unicode.rs:4:12
--> $DIR/unicode.rs:7:12
|
LL | print!("Here >​< is a ZWS, and ​another");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{200B}< is a ZWS, and /u{200B}another"`
|
= note: `-D clippy::invisible-characters` implied by `-D warnings`

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

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

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

error: literal non-ASCII character detected
--> $DIR/unicode.rs:20:12
--> $DIR/unicode.rs:25:16
|
LL | print!("Üben!");
| ^^^^^^^ help: consider replacing the string with: `"/u{dc}ben!"`
LL | print!("Üben!");
| ^^^^^^^ help: consider replacing the string with: `"/u{dc}ben!"`
|
= note: `-D clippy::non-ascii-literal` implied by `-D warnings`
note: the lint level is defined here
--> $DIR/unicode.rs:22:13
|
LL | #![deny(clippy::non_ascii_literal)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: literal non-ASCII character detected
--> $DIR/unicode.rs:31:36
|
LL | const _EMPTY_BLOCK: char = '▱';
| ^^^ help: consider replacing the string with: `'/u{25b1}'`

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

error: literal non-ASCII character detected
--> $DIR/unicode.rs:28:31
--> $DIR/unicode.rs:52:21
|
LL | let _ = "悲しいかな、ここに日本語を書くことはできない。";
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 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}"`
|
note: the lint level is defined here
--> $DIR/unicode.rs:41:17
|
LL | const _FULL_BLOCK: char = '▰';
| ^^^ help: consider replacing the string with: `'/u{25b0}'`
LL | #![deny(clippy::non_ascii_literal)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^

error: aborting due to 7 previous errors
error: aborting due to 8 previous errors

0 comments on commit f4f5bb4

Please sign in to comment.