Auto merge of #9327 - Serial-ATA:non_ascii_literal_macro, r=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: #7739
closes: #8263
This commit is contained in:
bors 2022-08-16 20:22:39 +00:00
commit f4f5bb4328
4 changed files with 110 additions and 39 deletions

View File

@ -1,5 +1,6 @@
use clippy_utils::diagnostics::span_lint_and_sugg; use clippy_utils::diagnostics::span_lint_and_sugg;
use clippy_utils::is_lint_allowed; use clippy_utils::is_lint_allowed;
use clippy_utils::macros::span_is_local;
use clippy_utils::source::snippet; use clippy_utils::source::snippet;
use rustc_ast::ast::LitKind; use rustc_ast::ast::LitKind;
use rustc_errors::Applicability; use rustc_errors::Applicability;
@ -98,6 +99,10 @@ fn escape<T: Iterator<Item = char>>(s: T) -> String {
} }
fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) { fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
if !span_is_local(span) {
return;
}
let string = snippet(cx, span, ""); let string = snippet(cx, span, "");
if string.chars().any(|c| ['\u{200B}', '\u{ad}', '\u{2060}'].contains(&c)) { if string.chars().any(|c| ['\u{200B}', '\u{ad}', '\u{2060}'].contains(&c)) {
span_lint_and_sugg( span_lint_and_sugg(
@ -113,6 +118,7 @@ fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
if string.chars().any(|c| c as u32 > 0x7F) { if string.chars().any(|c| c as u32 > 0x7F) {
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,
@ -128,6 +134,7 @@ fn check_str(cx: &LateContext<'_>, span: Span, id: HirId) {
Applicability::MachineApplicable, Applicability::MachineApplicable,
); );
} }
if is_lint_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) { if is_lint_allowed(cx, NON_ASCII_LITERAL, id) && string.chars().zip(string.nfc()).any(|(a, b)| a != b) {
span_lint_and_sugg( span_lint_and_sugg(
cx, cx,

View File

@ -1,4 +1,7 @@
// run-rustfix // run-rustfix
// compile-flags: --test
#![allow(dead_code)]
#[warn(clippy::invisible_characters)] #[warn(clippy::invisible_characters)]
fn zero() { fn zero() {
print!("Here >\u{200B}< is a ZWS, and \u{200B}another"); print!("Here >\u{200B}< is a ZWS, and \u{200B}another");
@ -15,22 +18,43 @@ fn canon() {
print!("a\u{0300}h?"); // also ok print!("a\u{0300}h?"); // also ok
} }
#[warn(clippy::non_ascii_literal)] mod non_ascii_literal {
fn uni() { #![deny(clippy::non_ascii_literal)]
print!("\u{dc}ben!");
print!("\u{DC}ben!"); // this is ok
}
// issue 8013 fn uni() {
#[warn(clippy::non_ascii_literal)] print!("\u{dc}ben!");
fn single_quote() { print!("\u{DC}ben!"); // this is ok
const _EMPTY_BLOCK: char = '\u{25b1}'; }
const _FULL_BLOCK: char = '\u{25b0}';
// 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 _ = "悲しいかな、ここに日本語を書くことはできない。";
}
#[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() { fn main() {
zero(); zero();
uni();
canon(); canon();
single_quote();
} }

View File

@ -1,4 +1,7 @@
// run-rustfix // run-rustfix
// compile-flags: --test
#![allow(dead_code)]
#[warn(clippy::invisible_characters)] #[warn(clippy::invisible_characters)]
fn zero() { fn zero() {
print!("Here >< is a ZWS, and another"); print!("Here >< is a ZWS, and another");
@ -15,22 +18,43 @@ fn canon() {
print!("a\u{0300}h?"); // also ok print!("a\u{0300}h?"); // also ok
} }
#[warn(clippy::non_ascii_literal)] mod non_ascii_literal {
fn uni() { #![deny(clippy::non_ascii_literal)]
print!("Üben!");
print!("\u{DC}ben!"); // this is ok
}
// issue 8013 fn uni() {
#[warn(clippy::non_ascii_literal)] print!("Üben!");
fn single_quote() { print!("\u{DC}ben!"); // this is ok
const _EMPTY_BLOCK: char = '▱'; }
const _FULL_BLOCK: char = '▰';
// 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 _ = "悲しいかな、ここに日本語を書くことはできない。";
}
#[test]
fn denied() {
let _ = "悲しいかな、ここに日本語を書くことはできない。";
}
}
} }
fn main() { fn main() {
zero(); zero();
uni();
canon(); canon();
single_quote();
} }

View File

@ -1,5 +1,5 @@
error: invisible character detected error: invisible character detected
--> $DIR/unicode.rs:4:12 --> $DIR/unicode.rs:7:12
| |
LL | print!("Here >< is a ZWS, and another"); LL | print!("Here >< is a ZWS, and another");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{200B}< is a ZWS, and /u{200B}another"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{200B}< is a ZWS, and /u{200B}another"`
@ -7,19 +7,19 @@ LL | print!("Here >< is a ZWS, and another");
= note: `-D clippy::invisible-characters` implied by `-D warnings` = note: `-D clippy::invisible-characters` implied by `-D warnings`
error: invisible character detected error: invisible character detected
--> $DIR/unicode.rs:6:12 --> $DIR/unicode.rs:9:12
| |
LL | print!("Here >­< is a SHY, and ­another"); LL | print!("Here >­< is a SHY, and ­another");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{AD}< is a SHY, and /u{AD}another"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{AD}< is a SHY, and /u{AD}another"`
error: invisible character detected error: invisible character detected
--> $DIR/unicode.rs:8:12 --> $DIR/unicode.rs:11:12
| |
LL | print!("Here >< is a WJ, and another"); LL | print!("Here >< is a WJ, and another");
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{2060}< is a WJ, and /u{2060}another"` | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider replacing the string with: `"Here >/u{2060}< is a WJ, and /u{2060}another"`
error: non-NFC Unicode sequence detected error: non-NFC Unicode sequence detected
--> $DIR/unicode.rs:14:12 --> $DIR/unicode.rs:17:12
| |
LL | print!("̀àh?"); LL | print!("̀àh?");
| ^^^^^ help: consider replacing the string with: `"̀àh?"` | ^^^^^ help: consider replacing the string with: `"̀àh?"`
@ -27,24 +27,40 @@ LL | print!("̀àh?");
= note: `-D clippy::unicode-not-nfc` implied by `-D warnings` = note: `-D clippy::unicode-not-nfc` implied by `-D warnings`
error: literal non-ASCII character detected error: literal non-ASCII character detected
--> $DIR/unicode.rs:20:12 --> $DIR/unicode.rs:25:16
| |
LL | print!("Üben!"); LL | print!("Üben!");
| ^^^^^^^ help: consider replacing the string with: `"/u{dc}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 error: literal non-ASCII character detected
--> $DIR/unicode.rs:27:32 --> $DIR/unicode.rs:31:36
| |
LL | const _EMPTY_BLOCK: char = '▱'; LL | const _EMPTY_BLOCK: char = '▱';
| ^^^ help: consider replacing the string with: `'/u{25b1}'` | ^^^ help: consider replacing the string with: `'/u{25b1}'`
error: literal non-ASCII character detected error: literal non-ASCII character detected
--> $DIR/unicode.rs:28:31 --> $DIR/unicode.rs:32:35
| |
LL | const _FULL_BLOCK: char = '▰'; LL | const _FULL_BLOCK: char = '▰';
| ^^^ help: consider replacing the string with: `'/u{25b0}'` | ^^^ help: consider replacing the string with: `'/u{25b0}'`
error: aborting due to 7 previous errors error: literal non-ASCII character detected
--> $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 | #![deny(clippy::non_ascii_literal)]
| ^^^^^^^^^^^^^^^^^^^^^^^^^
error: aborting due to 8 previous errors