mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-15 10:12:21 +00:00
Fix gen panics doc template for debug_assert
And add assert_eq, assert_ne, assert_matches support
Input:
```rust
pub fn $0foo(x: bool) {
debug_assert!(x);
}
```
Old:
```rust
/// .
///
/// # Panics
///
/// Panics if .
pub fn foo(x: bool) {
debug_assert!(x);
}
```
This PR fixes:
```rust
/// .
pub fn foo(x: bool) {
debug_assert!(x);
}
```
This commit is contained in:
parent
922e04a134
commit
2a030541fb
1 changed files with 62 additions and 6 deletions
|
|
@ -313,12 +313,28 @@ fn crate_name(ast_func: &ast::Fn, ctx: &AssistContext<'_>) -> Option<String> {
|
||||||
/// `None` if function without a body; some bool to guess if function can panic
|
/// `None` if function without a body; some bool to guess if function can panic
|
||||||
fn can_panic(ast_func: &ast::Fn) -> Option<bool> {
|
fn can_panic(ast_func: &ast::Fn) -> Option<bool> {
|
||||||
let body = ast_func.body()?.to_string();
|
let body = ast_func.body()?.to_string();
|
||||||
let can_panic = body.contains("panic!(")
|
let mut iter = body.chars();
|
||||||
// FIXME it would be better to not match `debug_assert*!` macro invocations
|
let assert_postfix = |s| {
|
||||||
|| body.contains("assert!(")
|
["!(", "_eq!(", "_ne!(", "_matches!("].iter().any(|postfix| str::starts_with(s, postfix))
|
||||||
|| body.contains(".unwrap()")
|
};
|
||||||
|| body.contains(".expect(");
|
|
||||||
Some(can_panic)
|
while !iter.as_str().is_empty() {
|
||||||
|
let s = iter.as_str();
|
||||||
|
iter.next();
|
||||||
|
if s.strip_prefix("debug_assert").is_some_and(assert_postfix) {
|
||||||
|
iter.nth(10);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
if s.strip_prefix("assert").is_some_and(assert_postfix)
|
||||||
|
|| s.starts_with("panic!(")
|
||||||
|
|| s.starts_with(".unwrap()")
|
||||||
|
|| s.starts_with(".expect(")
|
||||||
|
{
|
||||||
|
return Some(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
Some(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Helper function to get the name that should be given to `self` arguments
|
/// Helper function to get the name that should be given to `self` arguments
|
||||||
|
|
@ -677,6 +693,24 @@ pub fn panics_if(a: bool) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn guesses_debug_assert_macro_cannot_panic() {
|
||||||
|
check_assist(
|
||||||
|
generate_documentation_template,
|
||||||
|
r#"
|
||||||
|
pub fn $0debug_panics_if_not(a: bool) {
|
||||||
|
debug_assert!(a == true);
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
/// .
|
||||||
|
pub fn debug_panics_if_not(a: bool) {
|
||||||
|
debug_assert!(a == true);
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn guesses_assert_macro_can_panic() {
|
fn guesses_assert_macro_can_panic() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
|
@ -699,6 +733,28 @@ pub fn panics_if_not(a: bool) {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn guesses_assert_eq_macro_can_panic() {
|
||||||
|
check_assist(
|
||||||
|
generate_documentation_template,
|
||||||
|
r#"
|
||||||
|
pub fn $0panics_if_not(a: bool) {
|
||||||
|
assert_eq!(a, true);
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
r#"
|
||||||
|
/// .
|
||||||
|
///
|
||||||
|
/// # Panics
|
||||||
|
///
|
||||||
|
/// Panics if .
|
||||||
|
pub fn panics_if_not(a: bool) {
|
||||||
|
assert_eq!(a, true);
|
||||||
|
}
|
||||||
|
"#,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn guesses_unwrap_can_panic() {
|
fn guesses_unwrap_can_panic() {
|
||||||
check_assist(
|
check_assist(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue