Skip to content

Commit 687e5e7

Browse files
committed
Detect void function for the @return suggestion
1 parent 0c03f16 commit 687e5e7

3 files changed

Lines changed: 22 additions & 37 deletions

File tree

src/completion/phpdoc.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1972,8 +1972,11 @@ pub fn build_phpdoc_completions(
19721972
sort_text: Some(format!("0a_{}", def.tag.to_lowercase())),
19731973
..CompletionItem::default()
19741974
});
1975+
continue;
19751976
}
1976-
continue;
1977+
// Body has return-with-value statements — fall through
1978+
// to the generic `@return Type` fallback so the user
1979+
// can type the actual return type manually.
19771980
}
19781981
// Return type not detected — fall through to the generic
19791982
// `@return Type` fallback so the user can type it manually.

tests/completion_phpdoc.rs

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1324,8 +1324,8 @@ async fn test_phpdoc_smart_return_prefilled() {
13241324
assert_eq!(r.insert_text.as_deref(), Some("return string"));
13251325
}
13261326

1327-
/// @return with void return type and no return statements should suggest
1328-
/// `@return void` as a smart completion.
1327+
/// When a function has an explicit `: void` type hint, `@return` should
1328+
/// not be suggested at all — the type hint speaks for itself.
13291329
#[tokio::test]
13301330
async fn test_phpdoc_smart_return_void_generic() {
13311331
let backend = create_test_backend();
@@ -1343,25 +1343,16 @@ async fn test_phpdoc_smart_return_void_generic() {
13431343
let return_item = items
13441344
.iter()
13451345
.find(|i| i.filter_text.as_deref() == Some("@return"));
1346-
// void return with no return statements → suggest @return void
1346+
// Explicit `: void` type hint → @return is not needed
13471347
assert!(
1348-
return_item.is_some(),
1349-
"Should suggest @return void for void functions with no return statements"
1350-
);
1351-
assert_eq!(
1352-
return_item.unwrap().label,
1353-
"@return void",
1354-
"Label should be @return void"
1355-
);
1356-
assert_eq!(
1357-
return_item.unwrap().insert_text.as_deref(),
1358-
Some("return void"),
1359-
"Insert text should be 'return void'"
1348+
return_item.is_none(),
1349+
"Should NOT suggest @return when `: void` type hint is present. Got: {:?}",
1350+
return_item.map(|i| &i.label)
13601351
);
13611352
}
13621353

1363-
/// @return void should NOT be suggested when the void function contains
1364-
/// return statements with values (the void hint is likely wrong).
1354+
/// When an explicit `: void` type hint is present, `@return` should not
1355+
/// be suggested even if the body contains return statements with values.
13651356
#[tokio::test]
13661357
async fn test_phpdoc_smart_return_void_with_return_value() {
13671358
let backend = create_test_backend();
@@ -1383,13 +1374,13 @@ async fn test_phpdoc_smart_return_void_with_return_value() {
13831374
.find(|i| i.filter_text.as_deref() == Some("@return"));
13841375
assert!(
13851376
return_item.is_none(),
1386-
"Should NOT suggest @return void when body has return with value. Got: {:?}",
1377+
"Should NOT suggest @return when `: void` type hint is present. Got: {:?}",
13871378
return_item.map(|i| &i.label)
13881379
);
13891380
}
13901381

1391-
/// @return void SHOULD be suggested when the void function only has bare
1392-
/// `return;` statements (early returns with no value).
1382+
/// When an explicit `: void` type hint is present, `@return` should not
1383+
/// be suggested even with bare `return;` statements.
13931384
#[tokio::test]
13941385
async fn test_phpdoc_smart_return_void_with_bare_return() {
13951386
let backend = create_test_backend();
@@ -1413,10 +1404,10 @@ async fn test_phpdoc_smart_return_void_with_bare_return() {
14131404
.iter()
14141405
.find(|i| i.filter_text.as_deref() == Some("@return"));
14151406
assert!(
1416-
return_item.is_some(),
1417-
"Should suggest @return void when body only has bare return;"
1407+
return_item.is_none(),
1408+
"Should NOT suggest @return when `: void` type hint is present. Got: {:?}",
1409+
return_item.map(|i| &i.label)
14181410
);
1419-
assert_eq!(return_item.unwrap().label, "@return void");
14201411
}
14211412

14221413
/// @var should be pre-filled with the property type hint.

tests/phpdoc_internals.rs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1129,20 +1129,11 @@ fn smart_return_void_uses_no_return_item() {
11291129
let return_item = items
11301130
.iter()
11311131
.find(|i| i.filter_text.as_deref() == Some("@return"));
1132-
// void return with no return statements → suggest @return void
1132+
// Explicit `: void` type hint → @return is not needed
11331133
assert!(
1134-
return_item.is_some(),
1135-
"Should suggest @return void for void functions with no return statements"
1136-
);
1137-
assert_eq!(
1138-
return_item.unwrap().label,
1139-
"@return void",
1140-
"Label should be @return void"
1141-
);
1142-
assert_eq!(
1143-
return_item.unwrap().insert_text.as_deref(),
1144-
Some("return void"),
1145-
"Insert text should be 'return void'"
1134+
return_item.is_none(),
1135+
"Should NOT suggest @return when `: void` type hint is present. Got: {:?}",
1136+
return_item.map(|i| &i.label)
11461137
);
11471138
}
11481139

0 commit comments

Comments
 (0)