1 use crate::{EarlyContext, EarlyLintPass, LintContext};
3 use rustc_errors::{Applicability, SuggestionStyle};
4 use rustc_span::{BytePos, Span, Symbol};
7 /// The `text_direction_codepoint_in_literal` lint detects Unicode codepoints that change the
8 /// visual representation of text on screen in a way that does not correspond to their on
9 /// memory representation.
13 /// The unicode characters `\u{202A}`, `\u{202B}`, `\u{202D}`, `\u{202E}`, `\u{2066}`,
14 /// `\u{2067}`, `\u{2068}`, `\u{202C}` and `\u{2069}` make the flow of text on screen change
15 /// its direction on software that supports these codepoints. This makes the text "abc" display
16 /// as "cba" on screen. By leveraging software that supports these, people can write specially
17 /// crafted literals that make the surrounding code seem like it's performing one action, when
18 /// in reality it is performing another. Because of this, we proactively lint against their
19 /// presence to avoid surprises.
23 /// ```rust,compile_fail
24 /// #![deny(text_direction_codepoint_in_literal)]
26 /// println!("{:?}", '');
32 pub TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
34 "detect special Unicode codepoints that affect the visual representation of text on screen, \
35 changing the direction in which text flows",
38 declare_lint_pass!(HiddenUnicodeCodepoints => [TEXT_DIRECTION_CODEPOINT_IN_LITERAL]);
40 crate const UNICODE_TEXT_FLOW_CHARS: &[char] = &[
41 '\u{202A}', '\u{202B}', '\u{202D}', '\u{202E}', '\u{2066}', '\u{2067}', '\u{2068}', '\u{202C}',
45 impl HiddenUnicodeCodepoints {
46 fn lint_text_direction_codepoint(
48 cx: &EarlyContext<'_>,
52 point_at_inner_spans: bool,
55 // Obtain the `Span`s for each of the forbidden chars.
56 let spans: Vec<_> = text
59 .filter_map(|(i, c)| {
60 UNICODE_TEXT_FLOW_CHARS.contains(&c).then(|| {
61 let lo = span.lo() + BytePos(i as u32 + padding);
62 (c, span.with_lo(lo).with_hi(lo + BytePos(c.len_utf8() as u32)))
67 cx.struct_span_lint(TEXT_DIRECTION_CODEPOINT_IN_LITERAL, span, |lint| {
68 let mut err = lint.build(&format!(
69 "unicode codepoint changing visible direction of text present in {}",
72 let (an, s) = match spans.len() {
79 "this {} contains {}invisible unicode text flow control codepoint{}",
83 if point_at_inner_spans {
84 for (c, span) in &spans {
85 err.span_label(*span, format!("{:?}", c));
89 "these kind of unicode codepoints change the way text flows on applications that \
90 support them, but can cause confusion because they change the order of \
91 characters on the screen",
93 if point_at_inner_spans && !spans.is_empty() {
94 err.multipart_suggestion_with_style(
95 "if their presence wasn't intentional, you can remove them",
96 spans.iter().map(|(_, span)| (*span, "".to_string())).collect(),
97 Applicability::MachineApplicable,
98 SuggestionStyle::HideCodeAlways,
100 err.multipart_suggestion(
101 "if you want to keep them but make them visible in your source code, you can \
106 let c = format!("{:?}", c);
107 (span, c[1..c.len() - 1].to_string())
110 Applicability::MachineApplicable,
113 // FIXME: in other suggestions we've reversed the inner spans of doc comments. We
114 // should do the same here to provide the same good suggestions as we do for
116 err.note("if their presence wasn't intentional, you can remove them");
118 "if you want to keep them but make them visible in your source code, you can \
122 .map(|(c, _)| { format!("{:?}", c) })
123 .collect::<Vec<String>>()
131 impl EarlyLintPass for HiddenUnicodeCodepoints {
132 fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
133 if let ast::AttrKind::DocComment(_, comment) = attr.kind {
134 if comment.as_str().contains(UNICODE_TEXT_FLOW_CHARS) {
135 self.lint_text_direction_codepoint(cx, comment, attr.span, 0, false, "doc comment");
140 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
141 // byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
142 let (text, span, padding) = match &expr.kind {
143 ast::ExprKind::Lit(ast::Lit { token, kind, span }) => {
144 let text = token.symbol;
145 if !text.as_str().contains(UNICODE_TEXT_FLOW_CHARS) {
148 let padding = match kind {
149 // account for `"` or `'`
150 ast::LitKind::Str(_, ast::StrStyle::Cooked) | ast::LitKind::Char(_) => 1,
151 // account for `r###"`
152 ast::LitKind::Str(_, ast::StrStyle::Raw(val)) => *val as u32 + 2,
155 (text, span, padding)
159 self.lint_text_direction_codepoint(cx, text, *span, padding, true, "literal");