Skip to content

Commit 65f83e4

Browse files
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.
1 parent ee38991 commit 65f83e4

File tree

2 files changed

+24
-6
lines changed

2 files changed

+24
-6
lines changed

src/tools/rust-analyzer/crates/ide-diagnostics/src/handlers/incorrect_case.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -996,4 +996,17 @@ fn BAR() {
996996
"#,
997997
);
998998
}
999+
1000+
#[test]
1001+
fn allow_with_comment() {
1002+
check_diagnostics(
1003+
r#"
1004+
#[allow(
1005+
// Yo, sup
1006+
non_snake_case
1007+
)]
1008+
fn foo(_HelloWorld: ()) {}
1009+
"#,
1010+
);
1011+
}
9991012
}

src/tools/rust-analyzer/crates/syntax/src/hacks.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,15 @@ use crate::{ast, AstNode};
88

99
pub fn parse_expr_from_str(s: &str, edition: Edition) -> Option<ast::Expr> {
1010
let s = s.trim();
11-
let file = ast::SourceFile::parse(&format!("const _: () = {s};"), edition);
12-
let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?;
13-
if expr.syntax().text() != s {
14-
return None;
15-
}
16-
Some(expr)
11+
12+
let file = ast::SourceFile::parse(
13+
// Need a newline because the text may contain line comments.
14+
&format!("const _: () = ({s}\n);"),
15+
edition,
16+
);
17+
let expr = file.syntax_node().descendants().find_map(ast::ParenExpr::cast)?;
18+
// Can't check the text because the original text may contain whitespace and comments.
19+
// Wrap in parentheses to better allow for verification. Of course, the real fix is
20+
// to get rid of this hack.
21+
expr.expr()
1722
}

0 commit comments

Comments
 (0)