1 use crate::{EarlyContext, EarlyLintPass, LintContext};
2 use ast::util::unicode::{contains_text_flow_control_chars, TEXT_FLOW_CONTROL_CHARS};
4 use rustc_errors::{fluent, 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)))
64 TEXT_DIRECTION_CODEPOINT_IN_LITERAL,
66 fluent::lint_hidden_unicode_codepoints,
68 lint.set_arg("label", label);
69 lint.set_arg("count", spans.len());
70 lint.span_label(span, fluent::label);
71 lint.note(fluent::note);
72 if point_at_inner_spans {
73 for (c, span) in &spans {
74 lint.span_label(*span, format!("{:?}", c));
77 if point_at_inner_spans && !spans.is_empty() {
78 lint.multipart_suggestion_with_style(
79 fluent::suggestion_remove,
80 spans.iter().map(|(_, span)| (*span, "".to_string())).collect(),
81 Applicability::MachineApplicable,
82 SuggestionStyle::HideCodeAlways,
84 lint.multipart_suggestion(
85 fluent::suggestion_escape,
89 let c = format!("{:?}", c);
90 (span, c[1..c.len() - 1].to_string())
93 Applicability::MachineApplicable,
96 // FIXME: in other suggestions we've reversed the inner spans of doc comments. We
97 // should do the same here to provide the same good suggestions as we do for
103 .map(|(c, _)| format!("{:?}", c))
104 .collect::<Vec<String>>()
107 lint.note(fluent::suggestion_remove);
108 lint.note(fluent::no_suggestion_note_escape);
115 impl EarlyLintPass for HiddenUnicodeCodepoints {
116 fn check_attribute(&mut self, cx: &EarlyContext<'_>, attr: &ast::Attribute) {
117 if let ast::AttrKind::DocComment(_, comment) = attr.kind {
118 if contains_text_flow_control_chars(comment.as_str()) {
119 self.lint_text_direction_codepoint(cx, comment, attr.span, 0, false, "doc comment");
125 fn check_expr(&mut self, cx: &EarlyContext<'_>, expr: &ast::Expr) {
126 // byte strings are already handled well enough by `EscapeError::NonAsciiCharInByteString`
128 ast::ExprKind::Lit(token_lit) => {
129 let text = token_lit.symbol;
130 if !contains_text_flow_control_chars(text.as_str()) {
133 let padding = match token_lit.kind {
134 // account for `"` or `'`
135 ast::token::LitKind::Str | ast::token::LitKind::Char => 1,
136 // account for `r###"`
137 ast::token::LitKind::StrRaw(n) => n as u32 + 2,
140 self.lint_text_direction_codepoint(cx, text, expr.span, padding, true, "literal");