3 HiddenUnicodeCodepointsDiag, HiddenUnicodeCodepointsDiagLabels,
4 HiddenUnicodeCodepointsDiagSub,
6 EarlyContext, EarlyLintPass, LintContext,
8 use ast::util::unicode::{contains_text_flow_control_chars, TEXT_FLOW_CONTROL_CHARS};
10 use rustc_span::{BytePos, Span, Symbol};
13 /// The `text_direction_codepoint_in_literal` lint detects Unicode codepoints that change the
14 /// visual representation of text on screen in a way that does not correspond to their on
15 /// memory representation.
19 /// The unicode characters `\u{202A}`, `\u{202B}`, `\u{202D}`, `\u{202E}`, `\u{2066}`,
20 /// `\u{2067}`, `\u{2068}`, `\u{202C}` and `\u{2069}` make the flow of text on screen change
21 /// its direction on software that supports these codepoints. This makes the text "abc" display
22 /// as "cba" on screen. By leveraging software that supports these, people can write specially
23 /// crafted literals that make the surrounding code seem like it's performing one action, when
24 /// in reality it is performing another. Because of this, we proactively lint against their
25 /// presence to avoid surprises.
29 /// ```rust,compile_fail
30 /// #![deny(text_direction_codepoint_in_literal)]
32 /// println!("{:?}", '');
38 pub TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
40 "detect special Unicode codepoints that affect the visual representation of text on screen, \
41 changing the direction in which text flows",
44 declare_lint_pass!(HiddenUnicodeCodepoints => [TEXT_DIRECTION_CODEPOINT_IN_LITERAL]);
46 impl HiddenUnicodeCodepoints {
47 fn lint_text_direction_codepoint(
49 cx: &EarlyContext<'_>,
53 point_at_inner_spans: bool,
56 // Obtain the `Span`s for each of the forbidden chars.
57 let spans: Vec<_> = text
60 .filter_map(|(i, c)| {
61 TEXT_FLOW_CONTROL_CHARS.contains(&c).then(|| {
62 let lo = span.lo() + BytePos(i as u32 + padding);
63 (c, span.with_lo(lo).with_hi(lo + BytePos(c.len_utf8() as u32)))
68 let count = spans.len();
69 let labels = point_at_inner_spans
70 .then_some(HiddenUnicodeCodepointsDiagLabels { spans: spans.clone() });
71 let sub = if point_at_inner_spans && !spans.is_empty() {
72 HiddenUnicodeCodepointsDiagSub::Escape { spans }
74 HiddenUnicodeCodepointsDiagSub::NoEscape { spans }
78 TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
80 HiddenUnicodeCodepointsDiag { label, count, span_label: span, labels, sub },
84 impl EarlyLintPass for HiddenUnicodeCodepoints {
85 fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
86 if let ast::AttrKind::DocComment(_, comment) = attr.kind {
87 if contains_text_flow_control_chars(comment.as_str()) {
88 self.lint_text_direction_codepoint(cx, comment, attr.span, 0, false, "doc comment");
94 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
95 // byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
97 ast::ExprKind::Lit(token_lit) => {
98 let text = token_lit.symbol;
99 if !contains_text_flow_control_chars(text.as_str()) {
102 let padding = match token_lit.kind {
103 // account for `"` or `'`
104 ast::token::LitKind::Str | ast::token::LitKind::Char => 1,
105 // account for `r###"`
106 ast::token::LitKind::StrRaw(n) => n as u32 + 2,
109 self.lint_text_direction_codepoint(cx, text, expr.span, padding, true, "literal");