about summary refs log tree commit diff
path: root/src
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2024-09-19 13:27:21 +0000
committerbors <bors@rust-lang.org>2024-09-19 13:27:21 +0000
commit814da15d8baf2391f4d19cbc4bf73f81ffbeabdf (patch)
treee75196c516df2025ead9d4ce005588b9febc2efa /src
parent4ed7f4b5c970d7501f15a147d4cb6d940a8769e2 (diff)
parent65f83e47eeeba62a5eb32deedbc36eee812a1f6a (diff)
downloadrust-814da15d8baf2391f4d19cbc4bf73f81ffbeabdf.tar.gz
rust-814da15d8baf2391f4d19cbc4bf73f81ffbeabdf.zip
Auto merge of #18146 - ChayimFriedman2:allow-comment, r=Veykril
fix: Remove check that text of `parse_expr_from_str()` matches the produced parsed tree

This check is incorrect when we have comments and whitespace in the text.

We can strip comments, but then we still have whitespace, which we cannot strip without changing meaning for the parser. So instead I opt to remove the check, and wrap the expression in parentheses (asserting what produced is a parenthesized expression) to strengthen verification.

Fixes #18144.
Diffstat (limited to 'src')
-rw-r--r--src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_case.rs13
-rw-r--r--src/tools/rust-analyzer/crates/syntax/src/hacks.rs17
2 files changed, 24 insertions, 6 deletions
diff --git a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_case.rs b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_case.rs
index 96d8313cc1b..c4c28953175 100644
--- a/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_case.rs
+++ b/src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_case.rs
@@ -996,4 +996,17 @@ fn BAR() {
         "#,
         );
     }
+
+    #[test]
+    fn allow_with_comment() {
+        check_diagnostics(
+            r#"
+#[allow(
+    // Yo, sup
+    non_snake_case
+)]
+fn foo(_HelloWorld: ()) {}
+        "#,
+        );
+    }
 }
diff --git a/src/tools/rust-analyzer/crates/syntax/src/hacks.rs b/src/tools/rust-analyzer/crates/syntax/src/hacks.rs
index 9e63448ce96..2184359f1d0 100644
--- a/src/tools/rust-analyzer/crates/syntax/src/hacks.rs
+++ b/src/tools/rust-analyzer/crates/syntax/src/hacks.rs
@@ -8,10 +8,15 @@ use crate::{ast, AstNode};
 
 pub fn parse_expr_from_str(s: &str, edition: Edition) -> Option<ast::Expr> {
     let s = s.trim();
-    let file = ast::SourceFile::parse(&format!("const _: () = {s};"), edition);
-    let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?;
-    if expr.syntax().text() != s {
-        return None;
-    }
-    Some(expr)
+
+    let file = ast::SourceFile::parse(
+        // Need a newline because the text may contain line comments.
+        &format!("const _: () = ({s}\n);"),
+        edition,
+    );
+    let expr = file.syntax_node().descendants().find_map(ast::ParenExpr::cast)?;
+    // Can't check the text because the original text may contain whitespace and comments.
+    // Wrap in parentheses to better allow for verification. Of course, the real fix is
+    // to get rid of this hack.
+    expr.expr()
 }