Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve non ascii literal #4119

Merged
merged 3 commits into from May 27, 2019
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 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_help_and_lint, 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 @@ -99,7 +104,7 @@ fn check_str(cx: &LateContext<'_, '_>, span: Span, id: HirId) {
);
}
if string.chars().any(|c| c as u32 > 0x7F) {
span_help_and_lint(
span_lint_and_sugg(
cx,
NON_ASCII_LITERAL,
span,
Expand All @@ -112,6 +117,15 @@ fn check_str(cx: &LateContext<'_, '_>, span: Span, id: HirId) {
escape(string.nfc())
}
This conversation was marked as resolved.
Show resolved Hide resolved
),
format!(
"{}",
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) {
Expand Down