Unnamed repository; edit this file 'description' to name the repository.
-rw-r--r--crates/hir/src/lib.rs8
-rw-r--r--crates/ide-assists/src/handlers/inline_const_as_literal.rs695
-rw-r--r--crates/ide-assists/src/handlers/raw_string.rs278
-rw-r--r--crates/ide-assists/src/lib.rs3
-rw-r--r--crates/ide-assists/src/tests/generated.rs42
5 files changed, 727 insertions, 299 deletions
diff --git a/crates/hir/src/lib.rs b/crates/hir/src/lib.rs
index 5926d86542..294105dc02 100644
--- a/crates/hir/src/lib.rs
+++ b/crates/hir/src/lib.rs
@@ -3494,6 +3494,14 @@ impl Type {
}
}
+ pub fn is_scalar(&self) -> bool {
+ matches!(self.ty.kind(Interner), TyKind::Scalar(_))
+ }
+
+ pub fn is_tuple(&self) -> bool {
+ matches!(self.ty.kind(Interner), TyKind::Tuple(..))
+ }
+
pub fn remove_ref(&self) -> Option<Type> {
match &self.ty.kind(Interner) {
TyKind::Ref(.., ty) => Some(self.derived(ty.clone())),
diff --git a/crates/ide-assists/src/handlers/inline_const_as_literal.rs b/crates/ide-assists/src/handlers/inline_const_as_literal.rs
new file mode 100644
index 0000000000..2503fb0b99
--- /dev/null
+++ b/crates/ide-assists/src/handlers/inline_const_as_literal.rs
@@ -0,0 +1,695 @@
+use syntax::{ast, AstNode};
+
+use crate::{AssistContext, AssistId, AssistKind, Assists};
+
+// Assist: inline_const_as_literal
+//
+// Evaluate and inline const variable as literal.
+//
+// ```
+// const STRING: &str = "Hello, World!";
+//
+// fn something() -> &'static str {
+// STRING$0
+// }
+// ```
+// ->
+// ```
+// const STRING: &str = "Hello, World!";
+//
+// fn something() -> &'static str {
+// "Hello, World!"
+// }
+// ```
+pub(crate) fn inline_const_as_literal(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
+ let variable = ctx.find_node_at_offset::<ast::PathExpr>()?;
+
+ if let hir::PathResolution::Def(hir::ModuleDef::Const(konst)) =
+ ctx.sema.resolve_path(&variable.path()?)?
+ {
+ let konst_ty = konst.ty(ctx.sema.db);
+
+ let fuel = 20;
+
+ // FIXME: Add support to handle type aliases for builtin scalar types.
+ //
+ // There is no way to have a const static reference to a type that contains a interior
+ // mutability cell.
+ validate_type_recursively(ctx, Some(&konst_ty), false, fuel)?;
+
+ let expr = konst.value(ctx.sema.db)?;
+
+ // FIXME: Tuples and other sequence types will be inlined even though
+ // they might contain inner block expressions.
+ // e.g `(1, { 2 + 3 })` will be inline as it is.
+ let value = eval_and_inline_recursively(ctx, &konst, &expr, fuel)?;
+
+ let id = AssistId("inline_const_as_literal", AssistKind::RefactorInline);
+
+ let label = format!("Inline as literal");
+ let target = variable.syntax().text_range();
+
+ return acc.add(id, label, target, |edit| {
+ edit.replace(variable.syntax().text_range(), value);
+ });
+ }
+ None
+}
+
+fn eval_and_inline_recursively(
+ ctx: &AssistContext<'_>,
+ konst: &hir::Const,
+ expr: &ast::Expr,
+ fuel: i32,
+) -> Option<String> {
+ match (fuel > 0, expr) {
+ (true, ast::Expr::BlockExpr(block)) if block.is_standalone() => {
+ eval_and_inline_recursively(ctx, konst, &block.tail_expr()?, fuel - 1)
+ }
+ (_, ast::Expr::Literal(lit)) => Some(lit.to_string()),
+
+ // NOTE: For some expressions, `render_eval` will crash.
+ // e.g. `{ &[&[&[&[&[&[10, 20, 30]]]]]] }` will fail for `render_eval`.
+ //
+ // If these (Ref, Array or Tuple) contain evaluable expression, then we could
+ // visit/evaluate/walk them one by one, but I think this is enough for now.
+ //
+ // Add these if inlining without evaluation is preferred.
+ // (_, ast::Expr::RefExpr(expr)) => Some(expr.to_string()),
+ // (_, ast::Expr::ArrayExpr(expr)) => Some(expr.to_string()),
+ // (_, ast::Expr::TupleExpr(expr)) => Some(expr.to_string()),
+
+ // We should fail on (false, BlockExpr) && is_standalone because we've run out of fuel. BUT
+ // what is the harm in trying to evaluate the block anyway? Worst case would be that it
+ // behaves differently when for example: fuel = 1
+ // > const A: &[u32] = { &[10] }; OK because we inline ref expression.
+ // > const B: &[u32] = { { &[10] } }; ERROR because we evaluate block.
+ (_, ast::Expr::BlockExpr(_))
+ | (_, ast::Expr::RefExpr(_))
+ | (_, ast::Expr::ArrayExpr(_))
+ | (_, ast::Expr::TupleExpr(_))
+ | (_, ast::Expr::IfExpr(_))
+ | (_, ast::Expr::ParenExpr(_))
+ | (_, ast::Expr::MatchExpr(_))
+ | (_, ast::Expr::MacroExpr(_))
+ | (_, ast::Expr::BinExpr(_))
+ | (_, ast::Expr::CallExpr(_)) => match konst.render_eval(ctx.sema.db) {
+ Ok(result) => Some(result),
+ Err(_) => return None,
+ },
+ _ => return None,
+ }
+}
+
+fn validate_type_recursively(
+ ctx: &AssistContext<'_>,
+ ty_hir: Option<&hir::Type>,
+ refed: bool,
+ fuel: i32,
+) -> Option<()> {
+ match (fuel > 0, ty_hir) {
+ (true, Some(ty)) if ty.is_reference() => validate_type_recursively(
+ ctx,
+ ty.as_reference().map(|(ty, _)| ty).as_ref(),
+ true,
+ // FIXME: Saving fuel when `&` repeating. Might not be a good idea.
+ if refed { fuel } else { fuel - 1 },
+ ),
+ (true, Some(ty)) if ty.is_array() => validate_type_recursively(
+ ctx,
+ ty.as_array(ctx.db()).map(|(ty, _)| ty).as_ref(),
+ false,
+ fuel - 1,
+ ),
+ (true, Some(ty)) if ty.is_tuple() => ty
+ .tuple_fields(ctx.db())
+ .iter()
+ .all(|ty| validate_type_recursively(ctx, Some(ty), false, fuel - 1).is_some())
+ .then_some(()),
+ (true, Some(ty)) if refed && ty.is_slice() => {
+ validate_type_recursively(ctx, ty.as_slice().as_ref(), false, fuel - 1)
+ }
+ (_, Some(ty)) => match ty.as_builtin() {
+ // `const A: str` is not correct, `const A: &builtin` is always correct.
+ Some(builtin) if refed || (!refed && !builtin.is_str()) => Some(()),
+ _ => None,
+ },
+ _ => None,
+ }
+}
+
+#[cfg(test)]
+mod tests {
+ use super::*;
+ use crate::tests::{check_assist, check_assist_not_applicable};
+
+ const NUMBER: u8 = 1;
+ const BOOL: u8 = 2;
+ const STR: u8 = 4;
+ const CHAR: u8 = 8;
+
+ const TEST_PAIRS: &[(&str, &str, u8)] = &[
+ ("u8", "0", NUMBER),
+ ("u16", "0", NUMBER),
+ ("u32", "0", NUMBER),
+ ("u64", "0", NUMBER),
+ ("u128", "0", NUMBER),
+ ("usize", "0", NUMBER),
+ ("i8", "0", NUMBER),
+ ("i16", "0", NUMBER),
+ ("i32", "0", NUMBER),
+ ("i64", "0", NUMBER),
+ ("i128", "0", NUMBER),
+ ("isize", "0", NUMBER),
+ ("bool", "false", BOOL),
+ ("&str", "\"str\"", STR),
+ ("char", "'c'", CHAR),
+ ];
+
+ // -----------Not supported-----------
+
+ #[test]
+ fn inline_const_as_literal_const_fn_call_array() {
+ TEST_PAIRS.into_iter().for_each(|(ty, val, _)| {
+ check_assist_not_applicable(
+ inline_const_as_literal,
+ &format!(
+ r#"
+ const fn abc() -> [{ty}; 1] {{ [{val}] }}
+ const ABC: [{ty}; 1] = abc();
+ fn a() {{ A$0BC }}
+ "#
+ ),
+ );
+ });
+ }
+
+ #[test]
+ fn inline_const_as_literal_const_fn_call_slice() {
+ TEST_PAIRS.into_iter().for_each(|(ty, val, _)| {
+ check_assist_not_applicable(
+ inline_const_as_literal,
+ &format!(
+ r#"
+ const fn abc() -> &[{ty}] {{ &[{val}] }}
+ const ABC: &[{ty}] = abc();
+ fn a() {{ A$0BC }}
+ "#
+ ),
+ );
+ });
+ }
+
+ #[test]
+ fn inline_const_as_literal_expr_as_str_lit_not_applicable_const() {
+ check_assist_not_applicable(
+ inline_const_as_literal,
+ r#"
+ const STR$0ING: &str = "Hello, World!";
+
+ fn something() -> &'static str {
+ STRING
+ }
+ "#,
+ );
+ }
+
+ // ----------------------------
+
+ #[test]
+ fn inline_const_as_literal_const_expr() {
+ TEST_PAIRS.into_iter().for_each(|(ty, val, _)| {
+ check_assist(
+ inline_const_as_literal,
+ &format!(
+ r#"
+ const ABC: {ty} = {val};
+ fn a() {{ A$0BC }}
+ "#
+ ),
+ &format!(
+ r#"
+ const ABC: {ty} = {val};
+ fn a() {{ {val} }}
+ "#
+ ),
+ );
+ });
+ }
+
+ #[test]
+ fn inline_const_as_literal_const_block_expr() {
+ TEST_PAIRS.into_iter().for_each(|(ty, val, _)| {
+ check_assist(
+ inline_const_as_literal,
+ &format!(
+ r#"
+ const ABC: {ty} = {{ {val} }};
+ fn a() {{ A$0BC }}
+ "#
+ ),
+ &format!(
+ r#"
+ const ABC: {ty} = {{ {val} }};
+ fn a() {{ {val} }}
+ "#
+ ),
+ );
+ });
+ }
+
+ #[test]
+ fn inline_const_as_literal_const_block_eval_expr() {
+ TEST_PAIRS.into_iter().for_each(|(ty, val, _)| {
+ check_assist(
+ inline_const_as_literal,
+ &format!(
+ r#"
+ const ABC: {ty} = {{ true; {val} }};
+ fn a() {{ A$0BC }}
+ "#
+ ),
+ &format!(
+ r#"
+ const ABC: {ty} = {{ true; {val} }};
+ fn a() {{ {val} }}
+ "#
+ ),
+ );
+ });
+ }
+
+ #[test]
+ fn inline_const_as_literal_const_block_eval_block_expr() {
+ TEST_PAIRS.into_iter().for_each(|(ty, val, _)| {
+ check_assist(
+ inline_const_as_literal,
+ &format!(
+ r#"
+ const ABC: {ty} = {{ true; {{ {val} }} }};
+ fn a() {{ A$0BC }}
+ "#
+ ),
+ &format!(
+ r#"
+ const ABC: {ty} = {{ true; {{ {val} }} }};
+ fn a() {{ {val} }}
+ "#
+ ),
+ );
+ });
+ }
+
+ #[test]
+ fn inline_const_as_literal_const_fn_call_block_nested_builtin() {
+ TEST_PAIRS.into_iter().for_each(|(ty, val, _)| {
+ check_assist(
+ inline_const_as_literal,
+ &format!(
+ r#"
+ const fn abc() -> {ty} {{ {{ {{ {{ {val} }} }} }} }}
+ const ABC: {ty} = abc();
+ fn a() {{ A$0BC }}
+ "#
+ ),
+ &format!(
+ r#"
+ const fn abc() -> {ty} {{ {{ {{ {{ {val} }} }} }} }}
+ const ABC: {ty} = abc();
+ fn a() {{ {val} }}
+ "#
+ ),
+ );
+ });
+ }
+
+ #[test]
+ fn inline_const_as_literal_const_fn_call_tuple() {
+ // FIXME:
+ // const fn abc() -> (i32) { (1) }
+ // - results in `1` instead of `(1)`. It handles it as a paren expr
+ //
+ // const fn abc() -> (&str, &str) { ("a", "a") }
+ // - results in `("", "")` instead of `("a", "a")`.
+ TEST_PAIRS.into_iter().for_each(|(ty, val, bit)| {
+ // Everything except `&str`
+ if bit & STR == 0 {
+ check_assist(
+ inline_const_as_literal,
+ &format!(
+ r#"
+ const fn abc() -> ({ty}, {ty}) {{ ({val}, {val}) }}
+ const ABC: ({ty}, {ty}) = abc();
+ fn a() {{ A$0BC }}
+ "#
+ ),
+ &format!(
+ r#"
+ const fn abc() -> ({ty}, {ty}) {{ ({val}, {val}) }}
+ const ABC: ({ty}, {ty}) = abc();
+ fn a() {{ ({val}, {val}) }}
+ "#
+ ),
+ );
+ }
+ });
+ }
+
+ #[test]
+ fn inline_const_as_literal_const_fn_call_builtin() {
+ TEST_PAIRS.into_iter().for_each(|(ty, val, _)| {
+ check_assist(
+ inline_const_as_literal,
+ &format!(
+ r#"
+ const fn abc() -> {ty} {{ {val} }}
+ const ABC: {ty} = abc();
+ fn a() {{ A$0BC }}
+ "#
+ ),
+ &format!(
+ r#"
+ const fn abc() -> {ty} {{ {val} }}
+ const ABC: {ty} = abc();
+ fn a() {{ {val} }}
+ "#
+ ),
+ );
+ });
+ }
+
+ #[test]
+ fn inline_const_as_literal_scalar_operators() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const ABC: i32 = 1 + 2 + 3;
+ fn a() { A$0BC }
+ "#,
+ r#"
+ const ABC: i32 = 1 + 2 + 3;
+ fn a() { 6 }
+ "#,
+ );
+ }
+ #[test]
+ fn inline_const_as_literal_block_scalar_calculate_expr() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const ABC: i32 = { 1 + 2 + 3 };
+ fn a() { A$0BC }
+ "#,
+ r#"
+ const ABC: i32 = { 1 + 2 + 3 };
+ fn a() { 6 }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_block_scalar_calculate_param_expr() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const ABC: i32 = { (1 + 2 + 3) };
+ fn a() { A$0BC }
+ "#,
+ r#"
+ const ABC: i32 = { (1 + 2 + 3) };
+ fn a() { 6 }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_block_tuple_scalar_calculate_block_expr() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const ABC: (i32, i32) = { (1, { 2 + 3 }) };
+ fn a() { A$0BC }
+ "#,
+ r#"
+ const ABC: (i32, i32) = { (1, { 2 + 3 }) };
+ fn a() { (1, 5) }
+ "#,
+ );
+ }
+
+ // FIXME: These won't work with `konst.render_eval(ctx.sema.db)`
+ // #[test]
+ // fn inline_const_as_literal_block_slice() {
+ // check_assist(
+ // inline_const_as_literal,
+ // r#"
+ // const ABC: &[&[&[&[&[&[i32]]]]]] = { &[&[&[&[&[&[10, 20, 30]]]]]] };
+ // fn a() { A$0BC }
+ // "#,
+ // r#"
+ // const ABC: &[&[&[&[&[&[i32]]]]]] = { &[&[&[&[&[&[10, 20, 30]]]]]] };
+ // fn a() { &[&[&[&[&[&[10, 20, 30]]]]]] }
+ // "#,
+ // );
+ // }
+
+ // #[test]
+ // fn inline_const_as_literal_block_array() {
+ // check_assist(
+ // inline_const_as_literal,
+ // r#"
+ // const ABC: [[[i32; 1]; 1]; 1] = { [[[10]]] };
+ // fn a() { A$0BC }
+ // "#,
+ // r#"
+ // const ABC: [[[i32; 1]; 1]; 1] = { [[[10]]] };
+ // fn a() { [[[10]]] }
+ // "#,
+ // );
+ // }
+
+ // #[test]
+ // fn inline_const_as_literal_block_tuple() {
+ // check_assist(
+ // inline_const_as_literal,
+ // r#"
+ // const ABC: (([i32; 3]), (i32), ((&str, i32), i32), i32) = { (([1, 2, 3]), (10), (("hello", 10), 20), 30) };
+ // fn a() { A$0BC }
+ // "#,
+ // r#"
+ // const ABC: (([i32; 3]), (i32), ((&str, i32), i32), i32) = { (([1, 2, 3]), (10), (("hello", 10), 20), 30) };
+ // fn a() { (([1, 2, 3]), (10), (("hello", 10), 20), 30) }
+ // "#,
+ // );
+ // }
+
+ #[test]
+ fn inline_const_as_literal_block_recursive() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const ABC: &str = { { { { "hello" } } } };
+ fn a() { A$0BC }
+ "#,
+ r#"
+ const ABC: &str = { { { { "hello" } } } };
+ fn a() { "hello" }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_expr_as_str_lit() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const STRING: &str = "Hello, World!";
+
+ fn something() -> &'static str {
+ STR$0ING
+ }
+ "#,
+ r#"
+ const STRING: &str = "Hello, World!";
+
+ fn something() -> &'static str {
+ "Hello, World!"
+ }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_eval_const_block_expr_to_str_lit() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const STRING: &str = {
+ let x = 9;
+ if x + 10 == 21 {
+ "Hello, World!"
+ } else {
+ "World, Hello!"
+ }
+ };
+
+ fn something() -> &'static str {
+ STR$0ING
+ }
+ "#,
+ r#"
+ const STRING: &str = {
+ let x = 9;
+ if x + 10 == 21 {
+ "Hello, World!"
+ } else {
+ "World, Hello!"
+ }
+ };
+
+ fn something() -> &'static str {
+ "World, Hello!"
+ }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_eval_const_block_macro_expr_to_str_lit() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ macro_rules! co {() => {"World, Hello!"};}
+ const STRING: &str = { co!() };
+
+ fn something() -> &'static str {
+ STR$0ING
+ }
+ "#,
+ r#"
+ macro_rules! co {() => {"World, Hello!"};}
+ const STRING: &str = { co!() };
+
+ fn something() -> &'static str {
+ "World, Hello!"
+ }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_eval_const_match_expr_to_str_lit() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const STRING: &str = match 9 + 10 {
+ 0..18 => "Hello, World!",
+ _ => "World, Hello!"
+ };
+
+ fn something() -> &'static str {
+ STR$0ING
+ }
+ "#,
+ r#"
+ const STRING: &str = match 9 + 10 {
+ 0..18 => "Hello, World!",
+ _ => "World, Hello!"
+ };
+
+ fn something() -> &'static str {
+ "World, Hello!"
+ }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_eval_const_if_expr_to_str_lit() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const STRING: &str = if 1 + 2 == 4 {
+ "Hello, World!"
+ } else {
+ "World, Hello!"
+ }
+
+ fn something() -> &'static str {
+ STR$0ING
+ }
+ "#,
+ r#"
+ const STRING: &str = if 1 + 2 == 4 {
+ "Hello, World!"
+ } else {
+ "World, Hello!"
+ }
+
+ fn something() -> &'static str {
+ "World, Hello!"
+ }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_eval_const_macro_expr_to_str_lit() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ macro_rules! co {() => {"World, Hello!"};}
+ const STRING: &str = co!();
+
+ fn something() -> &'static str {
+ STR$0ING
+ }
+ "#,
+ r#"
+ macro_rules! co {() => {"World, Hello!"};}
+ const STRING: &str = co!();
+
+ fn something() -> &'static str {
+ "World, Hello!"
+ }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_eval_const_call_expr_to_str_lit() {
+ check_assist(
+ inline_const_as_literal,
+ r#"
+ const fn const_call() -> &'static str {"World, Hello!"}
+ const STRING: &str = const_call();
+
+ fn something() -> &'static str {
+ STR$0ING
+ }
+ "#,
+ r#"
+ const fn const_call() -> &'static str {"World, Hello!"}
+ const STRING: &str = const_call();
+
+ fn something() -> &'static str {
+ "World, Hello!"
+ }
+ "#,
+ );
+ }
+
+ #[test]
+ fn inline_const_as_literal_expr_as_str_lit_not_applicable() {
+ check_assist_not_applicable(
+ inline_const_as_literal,
+ r#"
+ const STRING: &str = "Hello, World!";
+
+ fn something() -> &'static str {
+ STRING $0
+ }
+ "#,
+ );
+ }
+}
diff --git a/crates/ide-assists/src/handlers/raw_string.rs b/crates/ide-assists/src/handlers/raw_string.rs
index 4852b0220e..63db606336 100644
--- a/crates/ide-assists/src/handlers/raw_string.rs
+++ b/crates/ide-assists/src/handlers/raw_string.rs
@@ -1,7 +1,6 @@
use std::borrow::Cow;
-use ide_db::syntax_helpers::insert_whitespace_into_node::insert_ws_into;
-use syntax::{ast, ast::IsString, AstNode, AstToken, TextRange, TextSize};
+use syntax::{ast, ast::IsString, AstToken, TextRange, TextSize};
use crate::{utils::required_hashes, AssistContext, AssistId, AssistKind, Assists};
@@ -157,72 +156,6 @@ pub(crate) fn remove_hash(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<
})
}
-// Assist: inline_str_literal
-//
-// Inline const variable as static str literal.
-//
-// ```
-// const STRING: &str = "Hello, World!";
-//
-// fn something() -> &'static str {
-// STR$0ING
-// }
-// ```
-// ->
-// ```
-// const STRING: &str = "Hello, World!";
-//
-// fn something() -> &'static str {
-// "Hello, World!"
-// }
-// ```
-pub(crate) fn inline_str_literal(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
- let variable = ctx.find_node_at_offset::<ast::PathExpr>()?;
-
- if let hir::PathResolution::Def(hir::ModuleDef::Const(konst)) =
- ctx.sema.resolve_path(&variable.path()?)?
- {
- if !konst.ty(ctx.sema.db).as_reference()?.0.as_builtin()?.is_str() {
- return None;
- }
-
- // FIXME: Make sure it's not possible to eval during diagnostic error
- let value = match konst.value(ctx.sema.db)? {
- ast::Expr::Literal(lit) => lit.to_string(),
- ast::Expr::BlockExpr(_)
- | ast::Expr::IfExpr(_)
- | ast::Expr::MatchExpr(_)
- | ast::Expr::CallExpr(_) => match konst.render_eval(ctx.sema.db) {
- Ok(result) => result,
- Err(_) => return None,
- },
- ast::Expr::MacroExpr(makro) => {
- let makro_call = makro.syntax().children().find_map(ast::MacroCall::cast)?;
- let makro_hir = ctx.sema.resolve_macro_call(&makro_call)?;
-
- // This should not be necessary because of the `makro_call` check
- if !makro_hir.is_fn_like(ctx.sema.db) {
- return None;
- }
-
- // FIXME: Make procedural/build-in macro tests
- insert_ws_into(ctx.sema.expand(&makro_call)?).to_string()
- }
- _ => return None,
- };
-
- let id = AssistId("inline_str_literal", AssistKind::RefactorInline);
- let label = "Inline as static `&str` literal";
- let target = variable.syntax().text_range();
-
- acc.add(id, label, target, |edit| {
- edit.replace(variable.syntax().text_range(), value);
- });
- }
-
- Some(())
-}
-
#[cfg(test)]
mod tests {
use super::*;
@@ -549,213 +482,4 @@ string"###;
"#,
);
}
-
- #[test]
- fn inline_expr_as_str_lit() {
- check_assist(
- inline_str_literal,
- r#"
- const STRING: &str = "Hello, World!";
-
- fn something() -> &'static str {
- STR$0ING
- }
- "#,
- r#"
- const STRING: &str = "Hello, World!";
-
- fn something() -> &'static str {
- "Hello, World!"
- }
- "#,
- );
- }
-
- #[test]
- fn inline_eval_const_block_expr_to_str_lit() {
- check_assist(
- inline_str_literal,
- r#"
- const STRING: &str = {
- let x = 9;
- if x + 10 == 21 {
- "Hello, World!"
- } else {
- "World, Hello!"
- }
- };
-
- fn something() -> &'static str {
- STR$0ING
- }
- "#,
- r#"
- const STRING: &str = {
- let x = 9;
- if x + 10 == 21 {
- "Hello, World!"
- } else {
- "World, Hello!"
- }
- };
-
- fn something() -> &'static str {
- "World, Hello!"
- }
- "#,
- );
- }
-
- #[test]
- fn inline_eval_const_block_macro_expr_to_str_lit() {
- check_assist(
- inline_str_literal,
- r#"
- macro_rules! co {() => {"World, Hello!"};}
- const STRING: &str = { co!() };
-
- fn something() -> &'static str {
- STR$0ING
- }
- "#,
- r#"
- macro_rules! co {() => {"World, Hello!"};}
- const STRING: &str = { co!() };
-
- fn something() -> &'static str {
- "World, Hello!"
- }
- "#,
- );
- }
-
- #[test]
- fn inline_eval_const_match_expr_to_str_lit() {
- check_assist(
- inline_str_literal,
- r#"
- const STRING: &str = match 9 + 10 {
- 0..18 => "Hello, World!",
- _ => "World, Hello!"
- };
-
- fn something() -> &'static str {
- STR$0ING
- }
- "#,
- r#"
- const STRING: &str = match 9 + 10 {
- 0..18 => "Hello, World!",
- _ => "World, Hello!"
- };
-
- fn something() -> &'static str {
- "World, Hello!"
- }
- "#,
- );
- }
-
- #[test]
- fn inline_eval_const_if_expr_to_str_lit() {
- check_assist(
- inline_str_literal,
- r#"
- const STRING: &str = if 1 + 2 == 4 {
- "Hello, World!"
- } else {
- "World, Hello!"
- }
-
- fn something() -> &'static str {
- STR$0ING
- }
- "#,
- r#"
- const STRING: &str = if 1 + 2 == 4 {
- "Hello, World!"
- } else {
- "World, Hello!"
- }
-
- fn something() -> &'static str {
- "World, Hello!"
- }
- "#,
- );
- }
-
- #[test]
- fn inline_eval_const_macro_expr_to_str_lit() {
- check_assist(
- inline_str_literal,
- r#"
- macro_rules! co {() => {"World, Hello!"};}
- const STRING: &str = co!();
-
- fn something() -> &'static str {
- STR$0ING
- }
- "#,
- r#"
- macro_rules! co {() => {"World, Hello!"};}
- const STRING: &str = co!();
-
- fn something() -> &'static str {
- "World, Hello!"
- }
- "#,
- );
- }
-
- #[test]
- fn inline_eval_const_call_expr_to_str_lit() {
- check_assist(
- inline_str_literal,
- r#"
- const fn const_call() -> &'static str {"World, Hello!"}
- const STRING: &str = const_call();
-
- fn something() -> &'static str {
- STR$0ING
- }
- "#,
- r#"
- const fn const_call() -> &'static str {"World, Hello!"}
- const STRING: &str = const_call();
-
- fn something() -> &'static str {
- "World, Hello!"
- }
- "#,
- );
- }
-
- #[test]
- fn inline_expr_as_str_lit_not_applicable() {
- check_assist_not_applicable(
- inline_str_literal,
- r#"
- const STRING: &str = "Hello, World!";
-
- fn something() -> &'static str {
- STRING $0
- }
- "#,
- );
- }
-
- #[test]
- fn inline_expr_as_str_lit_not_applicable_const() {
- check_assist_not_applicable(
- inline_str_literal,
- r#"
- const STR$0ING: &str = "Hello, World!";
-
- fn something() -> &'static str {
- STRING
- }
- "#,
- );
- }
}
diff --git a/crates/ide-assists/src/lib.rs b/crates/ide-assists/src/lib.rs
index bf6b4fdf85..111753bf30 100644
--- a/crates/ide-assists/src/lib.rs
+++ b/crates/ide-assists/src/lib.rs
@@ -161,6 +161,7 @@ mod handlers {
mod generate_delegate_methods;
mod add_return_type;
mod inline_call;
+ mod inline_const_as_literal;
mod inline_local_variable;
mod inline_macro;
mod inline_type_alias;
@@ -265,6 +266,7 @@ mod handlers {
generate_new::generate_new,
inline_call::inline_call,
inline_call::inline_into_callers,
+ inline_const_as_literal::inline_const_as_literal,
inline_local_variable::inline_local_variable,
inline_type_alias::inline_type_alias,
inline_type_alias::inline_type_alias_uses,
@@ -288,7 +290,6 @@ mod handlers {
raw_string::add_hash,
raw_string::make_usual_string,
raw_string::remove_hash,
- raw_string::inline_str_literal,
remove_mut::remove_mut,
remove_unused_param::remove_unused_param,
remove_parentheses::remove_parentheses,
diff --git a/crates/ide-assists/src/tests/generated.rs b/crates/ide-assists/src/tests/generated.rs
index 3249b6a25e..c097e07398 100644
--- a/crates/ide-assists/src/tests/generated.rs
+++ b/crates/ide-assists/src/tests/generated.rs
@@ -1480,6 +1480,27 @@ fn foo(name: Option<&str>) {
}
#[test]
+fn doctest_inline_const_as_literal() {
+ check_doc_test(
+ "inline_const_as_literal",
+ r#####"
+const STRING: &str = "Hello, World!";
+
+fn something() -> &'static str {
+ STRING$0
+}
+"#####,
+ r#####"
+const STRING: &str = "Hello, World!";
+
+fn something() -> &'static str {
+ "Hello, World!"
+}
+"#####,
+ )
+}
+
+#[test]
fn doctest_inline_into_callers() {
check_doc_test(
"inline_into_callers",
@@ -1568,27 +1589,6 @@ fn main() {
}
#[test]
-fn doctest_inline_str_literal() {
- check_doc_test(
- "inline_str_literal",
- r#####"
-const STRING: &str = "Hello, World!";
-
-fn something() -> &'static str {
- STR$0ING
-}
-"#####,
- r#####"
-const STRING: &str = "Hello, World!";
-
-fn something() -> &'static str {
- "Hello, World!"
-}
-"#####,
- )
-}
-
-#[test]
fn doctest_inline_type_alias() {
check_doc_test(
"inline_type_alias",