summaryrefslogtreecommitdiffstats
path: root/src/tools/rust-analyzer/crates/syntax/src/hacks.rs
blob: ec3d3d444c365822f5c9d7efea9e8268f1bb36b1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//! Things which exist to solve practical issues, but which shouldn't exist.
//!
//! Please avoid adding new usages of the functions in this module

use crate::{ast, AstNode};

pub fn parse_expr_from_str(s: &str) -> Option<ast::Expr> {
    let s = s.trim();
    let file = ast::SourceFile::parse(&format!("const _: () = {};", s));
    let expr = file.syntax_node().descendants().find_map(ast::Expr::cast)?;
    if expr.syntax().text() != s {
        return None;
    }
    Some(expr)
}