-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: warn if parameters or placeholders are unused
- Loading branch information
1 parent
de863f8
commit 928999a
Showing
9 changed files
with
182 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
src/language/validation/other/declarations/placeholders.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import {isSdsBlock, isSdsStatement, SdsPlaceholder} from '../../../generated/ast.js'; | ||
import {getContainerOfType, ValidationAcceptor} from 'langium'; | ||
import { SafeDsServices } from '../../../safe-ds-module.js'; | ||
import {statementsOrEmpty} from "../../../helpers/nodeProperties.js"; | ||
import {last} from "radash"; | ||
|
||
export const CODE_PLACEHOLDER_UNUSED = 'placeholder/unused'; | ||
|
||
export const placeholderShouldBeUsed = | ||
(services: SafeDsServices) => (node: SdsPlaceholder, accept: ValidationAcceptor) => { | ||
const usages = services.helpers.NodeMapper.placeholderToReferences(node); | ||
if (!usages.isEmpty()) { | ||
return; | ||
} | ||
|
||
// Don't show a warning if the placeholder is declared in the last statement in the block | ||
const containingStatement = getContainerOfType(node, isSdsStatement); | ||
const containingBlock = getContainerOfType(containingStatement, isSdsBlock); | ||
const allStatementsInBlock = statementsOrEmpty(containingBlock); | ||
if (last(allStatementsInBlock) === containingStatement) { | ||
return; | ||
} | ||
|
||
accept('warning', 'This placeholder is unused and can be removed.', { | ||
node, | ||
property: 'name', | ||
code: CODE_PLACEHOLDER_UNUSED, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
tests/resources/validation/other/declarations/parameter/unused/main.sdstest
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package tests.validation.declarations.parameters.unused | ||
|
||
segment mySegment( | ||
// $TEST$ warning "This parameter is unused and can be removed." | ||
»unused«: Int, | ||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
»used«: Int | ||
) { | ||
used; | ||
|
||
/* | ||
* Since we only allow lambdas as arguments, there signature is predefined, so no warning should be emitted. | ||
*/ | ||
|
||
( | ||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
»unused«: Int, | ||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
»used«: Int | ||
) { | ||
used; | ||
}; | ||
|
||
( | ||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
»unused«: Int, | ||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
»used«: Int | ||
) -> used; | ||
} | ||
|
||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
annotation MyAnnotation(»unused«: Int) | ||
|
||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
class MyClass(»unused«: Int) | ||
|
||
enum MyEnum { | ||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
MyEnumVariant(»unused«: Int) | ||
} | ||
|
||
fun myFunction( | ||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
»unused«: Int, | ||
|
||
// $TEST$ no warning "This parameter is unused and can be removed." | ||
myCallableType: (»unused«: Int) -> (), | ||
) |
Oops, something went wrong.