-
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 statement has no effect (#787)
Closes #664 ### Summary of Changes Show a warning if a statement has no effect.
- Loading branch information
1 parent
cd4f2c1
commit 6f45dc4
Showing
6 changed files
with
102 additions
and
161 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
57 changes: 57 additions & 0 deletions
57
packages/safe-ds-lang/src/language/validation/other/statements/statements.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,57 @@ | ||
import { | ||
isSdsAssignment, | ||
isSdsExpressionStatement, | ||
isSdsWildcard, | ||
SdsExpression, | ||
SdsStatement, | ||
} from '../../../generated/ast.js'; | ||
import { ValidationAcceptor } from 'langium'; | ||
import { SafeDsServices } from '../../../safe-ds-module.js'; | ||
import { getAssignees } from '../../../helpers/nodeProperties.js'; | ||
|
||
export const CODE_STATEMENT_HAS_NO_EFFECT = 'statement/has-no-effect'; | ||
|
||
export const statementMustDoSomething = (services: SafeDsServices) => { | ||
const statementDoesSomething = statementDoesSomethingProvider(services); | ||
|
||
return (node: SdsStatement, accept: ValidationAcceptor): void => { | ||
if (!statementDoesSomething(node)) { | ||
accept('warning', 'This statement does nothing.', { | ||
node, | ||
code: CODE_STATEMENT_HAS_NO_EFFECT, | ||
}); | ||
} | ||
}; | ||
}; | ||
|
||
const statementDoesSomethingProvider = (services: SafeDsServices) => { | ||
const statementExpressionDoesSomething = statementExpressionDoesSomethingProvider(services); | ||
|
||
return (node: SdsStatement): boolean => { | ||
if (isSdsAssignment(node)) { | ||
return !getAssignees(node).every(isSdsWildcard) || statementExpressionDoesSomething(node.expression); | ||
} else if (isSdsExpressionStatement(node)) { | ||
return statementExpressionDoesSomething(node.expression); | ||
} else { | ||
/* c8 ignore next 2 */ | ||
return false; | ||
} | ||
}; | ||
}; | ||
|
||
const statementExpressionDoesSomethingProvider = (services: SafeDsServices) => { | ||
const callGraphComputer = services.flow.CallGraphComputer; | ||
const purityComputer = services.purity.PurityComputer; | ||
|
||
return (node: SdsExpression | undefined): boolean => { | ||
if (!node) { | ||
/* c8 ignore next 2 */ | ||
return false; | ||
} | ||
|
||
return ( | ||
callGraphComputer.getAllContainedCalls(node).some((it) => callGraphComputer.isRecursive(it)) || | ||
!purityComputer.isPureExpression(node) | ||
); | ||
}; | ||
}; |
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
4 changes: 3 additions & 1 deletion
4
.../skip-old/assignments/hasNoEffect.sdstest → ...ts/assignments/has no effect/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
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
147 changes: 0 additions & 147 deletions
147
packages/safe-ds-lang/tests/resources/validation/skip-old/staticAnalysis/sideEffects.sdstest
This file was deleted.
Oops, something went wrong.