1 use crate::{EarlyContext, EarlyLintPass, LintContext};
2 use ast::util::unicode::{contains_text_flow_control_chars, TEXT_FLOW_CONTROL_CHARS};
4 use rustc_errors::{Applicability, SuggestionStyle};
5 use rustc_span::{BytePos, Span, Symbol};
8 /// The `text_direction_codepoint_in_literal` lint detects Unicode codepoints that change the
9 /// visual representation of text on screen in a way that does not correspond to their on
10 /// memory representation.
14 /// The unicode characters `\u{202A}`, `\u{202B}`, `\u{202D}`, `\u{202E}`, `\u{2066}`,
15 /// `\u{2067}`, `\u{2068}`, `\u{202C}` and `\u{2069}` make the flow of text on screen change
16 /// its direction on software that supports these codepoints. This makes the text "abc" display
17 /// as "cba" on screen. By leveraging software that supports these, people can write specially
18 /// crafted literals that make the surrounding code seem like it's performing one action, when
19 /// in reality it is performing another. Because of this, we proactively lint against their
20 /// presence to avoid surprises.
24 /// ```rust,compile_fail
25 /// #![deny(text_direction_codepoint_in_literal)]
27 /// println!("{:?}", '');
33 pub TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
35 "detect special Unicode codepoints that affect the visual representation of text on screen, \
36 changing the direction in which text flows",
39 declare_lint_pass!(HiddenUnicodeCodepoints => [TEXT_DIRECTION_CODEPOINT_IN_LITERAL]);
41 impl HiddenUnicodeCodepoints {
42 fn lint_text_direction_codepoint(
44 cx: &EarlyContext<'_>,
48 point_at_inner_spans: bool,
51 // Obtain the `Span`s for each of the forbidden chars.
52 let spans: Vec<_> = text
55 .filter_map(|(i, c)| {
56 TEXT_FLOW_CONTROL_CHARS.contains(&c).then(|| {
57 let lo = span.lo() + BytePos(i as u32 + padding);
58 (c, span.with_lo(lo).with_hi(lo + BytePos(c.len_utf8() as u32)))
63 cx.struct_span_lint(TEXT_DIRECTION_CODEPOINT_IN_LITERAL, span, |lint| {
64 let mut err = lint.build(&format!(
65 "unicode codepoint changing visible direction of text present in {}",
68 let (an, s) = match spans.len() {
75 "this {} contains {}invisible unicode text flow control codepoint{}",
79 if point_at_inner_spans {
80 for (c, span) in &spans {
81 err.span_label(*span, format!("{:?}", c));
85 "these kind of unicode codepoints change the way text flows on applications that \
86 support them, but can cause confusion because they change the order of \
87 characters on the screen",
89 if point_at_inner_spans && !spans.is_empty() {
90 err.multipart_suggestion_with_style(
91 "if their presence wasn't intentional, you can remove them",
92 spans.iter().map(|(_, span)| (*span, "".to_string())).collect(),
93 Applicability::MachineApplicable,
94 SuggestionStyle::HideCodeAlways,
96 err.multipart_suggestion(
97 "if you want to keep them but make them visible in your source code, you can \
102 let c = format!("{:?}", c);
103 (span, c[1..c.len() - 1].to_string())
106 Applicability::MachineApplicable,
109 // FIXME: in other suggestions we've reversed the inner spans of doc comments. We
110 // should do the same here to provide the same good suggestions as we do for
112 err.note("if their presence wasn't intentional, you can remove them");
114 "if you want to keep them but make them visible in your source code, you can \
118 .map(|(c, _)| { format!("{:?}", c) })
119 .collect::<Vec<String>>()
127 impl EarlyLintPass for HiddenUnicodeCodepoints {
128 fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
129 if let ast::AttrKind::DocComment(_, comment) = attr.kind {
130 if contains_text_flow_control_chars(comment.as_str()) {
131 self.lint_text_direction_codepoint(cx, comment, attr.span, 0, false, "doc comment");
136 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
137 // byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
138 let (text, span, padding) = match &expr.kind {
139 ast::ExprKind::Lit(ast::Lit { token, kind, span }) => {
140 let text = token.symbol;
141 if !contains_text_flow_control_chars(text.as_str()) {
144 let padding = match kind {
145 // account for `"` or `'`
146 ast::LitKind::Str(_, ast::StrStyle::Cooked) | ast::LitKind::Char(_) => 1,
147 // account for `r###"`
148 ast::LitKind::Str(_, ast::StrStyle::Raw(val)) => *val as u32 + 2,
151 (text, span, padding)
155 self.lint_text_direction_codepoint(cx, text, *span, padding, true, "literal");