diff --git a/src/language/validation/other/declarations/annotations.ts b/src/language/validation/other/declarations/annotations.ts deleted file mode 100644 index 10816a726..000000000 --- a/src/language/validation/other/declarations/annotations.ts +++ /dev/null @@ -1,17 +0,0 @@ -import { SdsAnnotation } from '../../../generated/ast.js'; -import { ValidationAcceptor } from 'langium'; -import { parametersOrEmpty } from '../../../helpers/nodeProperties.js'; - -export const CODE_ANNOTATION_PARAMETER_CONST_MODIFIER = 'annotation/parameter-const-modifier'; - -export const annotationParameterShouldNotHaveConstModifier = (node: SdsAnnotation, accept: ValidationAcceptor) => { - for (const parameter of parametersOrEmpty(node)) { - if (parameter.isConstant) { - accept('info', 'Parameters of annotations implicitly have the const modifier.', { - node: parameter, - property: 'name', - code: CODE_ANNOTATION_PARAMETER_CONST_MODIFIER, - }); - } - } -}; diff --git a/src/language/validation/other/expressions/lambdas.ts b/src/language/validation/other/expressions/lambdas.ts index a2b68f200..5a56ec1a6 100644 --- a/src/language/validation/other/expressions/lambdas.ts +++ b/src/language/validation/other/expressions/lambdas.ts @@ -9,7 +9,7 @@ export const lambdaParameterMustNotHaveConstModifier = (node: SdsLambda, accept: if (parameter.isConstant) { accept('error', 'The const modifier is not applicable to parameters of lambdas.', { node: parameter, - property: 'name', + property: 'isConstant', code: CODE_LAMBDA_CONST_MODIFIER, }); } diff --git a/src/language/validation/other/types/callableTypes.ts b/src/language/validation/other/types/callableTypes.ts index 52a7d79e7..104b88031 100644 --- a/src/language/validation/other/types/callableTypes.ts +++ b/src/language/validation/other/types/callableTypes.ts @@ -14,7 +14,7 @@ export const callableTypeParameterMustNotHaveConstModifier = ( if (parameter.isConstant) { accept('error', 'The const modifier is not applicable to parameters of callable types.', { node: parameter, - property: 'name', + property: 'isConstant', code: CODE_CALLABLE_TYPE_CONST_MODIFIER, }); } diff --git a/src/language/validation/safe-ds-validator.ts b/src/language/validation/safe-ds-validator.ts index ab77827e5..0ec0351c1 100644 --- a/src/language/validation/safe-ds-validator.ts +++ b/src/language/validation/safe-ds-validator.ts @@ -18,6 +18,7 @@ import { import { annotationCallArgumentListShouldBeNeeded, annotationParameterListShouldNotBeEmpty, + annotationParameterShouldNotHaveConstModifier, assignmentShouldHaveMoreThanWildcardsAsAssignees, callArgumentListShouldBeNeeded, classBodyShouldNotBeEmpty, @@ -61,7 +62,6 @@ import { } from './builtins/experimental.js'; import { placeholderShouldBeUsed } from './other/declarations/placeholders.js'; import { segmentParameterShouldBeUsed, segmentResultMustBeAssignedExactlyOnce } from './other/declarations/segments.js'; -import { annotationParameterShouldNotHaveConstModifier } from './other/declarations/annotations.js'; import { lambdaParameterMustNotHaveConstModifier } from './other/expressions/lambdas.js'; /** diff --git a/src/language/validation/style.ts b/src/language/validation/style.ts index 1e0b5c414..d38896322 100644 --- a/src/language/validation/style.ts +++ b/src/language/validation/style.ts @@ -25,6 +25,7 @@ import { UnknownType } from '../typing/model.js'; export const CODE_STYLE_UNNECESSARY_ASSIGNMENT = 'style/unnecessary-assignment'; export const CODE_STYLE_UNNECESSARY_ARGUMENT_LIST = 'style/unnecessary-argument-list'; export const CODE_STYLE_UNNECESSARY_BODY = 'style/unnecessary-body'; +export const CODE_STYLE_UNNECESSARY_CONST_MODIFIER = 'style/unnecessary-const-modifier'; export const CODE_STYLE_UNNECESSARY_CONSTRAINT_LIST = 'style/unnecessary-constraint-list'; export const CODE_STYLE_UNNECESSARY_ELVIS_OPERATOR = 'style/unnecessary-elvis-operator'; export const CODE_STYLE_UNNECESSARY_PARAMETER_LIST = 'style/unnecessary-parameter-list'; @@ -120,6 +121,22 @@ export const enumBodyShouldNotBeEmpty = (node: SdsEnumBody, accept: ValidationAc } }; +// ----------------------------------------------------------------------------- +// Unnecessary const modifier +// ----------------------------------------------------------------------------- + +export const annotationParameterShouldNotHaveConstModifier = (node: SdsAnnotation, accept: ValidationAcceptor) => { + for (const parameter of parametersOrEmpty(node)) { + if (parameter.isConstant) { + accept('info', 'Annotation parameters are always const, so this modifier can be removed.', { + node: parameter, + property: 'isConstant', + code: CODE_STYLE_UNNECESSARY_CONST_MODIFIER, + }); + } + } +}; + // ----------------------------------------------------------------------------- // Unnecessary constraint lists // ----------------------------------------------------------------------------- @@ -224,7 +241,7 @@ export const namedTypeTypeArgumentListShouldBeNeeded = (node: SdsNamedType, acce if (isEmpty(typeParametersOrEmpty(namedTypeDeclaration))) { accept('info', 'This type argument list can be removed.', { node: typeArgumentList, - code: CODE_STYLE_UNNECESSARY_ARGUMENT_LIST, + code: CODE_STYLE_UNNECESSARY_TYPE_ARGUMENT_LIST, }); } }; diff --git a/tests/resources/validation/other/declarations/parameters/const modifier/error.sdstest b/tests/resources/validation/other/declarations/parameters/const modifier/error.sdstest new file mode 100644 index 000000000..891b86531 --- /dev/null +++ b/tests/resources/validation/other/declarations/parameters/const modifier/error.sdstest @@ -0,0 +1,20 @@ +package tests.validation.other.declarations.parameters.illegalConstModifier + +fun f( + g: ( + // $TEST$ error "The const modifier is not applicable to parameters of callable types." + »const« p1: Int + ) -> () +) + +pipeline myPipeline { + ( + // $TEST$ error "The const modifier is not applicable to parameters of lambdas." + »const« p1: Int + ) {}; + + ( + // $TEST$ error "The const modifier is not applicable to parameters of lambdas." + »const« p1: Int + ) -> 1; +} diff --git a/tests/resources/validation/other/declarations/parameters/const modifier/main.sdstest b/tests/resources/validation/other/declarations/parameters/const modifier/main.sdstest deleted file mode 100644 index f09637711..000000000 --- a/tests/resources/validation/other/declarations/parameters/const modifier/main.sdstest +++ /dev/null @@ -1,37 +0,0 @@ -package tests.validation.other.declarations.parameters.illegalConstModifier - -annotation MyAnnotation( - // $TEST$ info "Parameters of annotations implicitly have the const modifier." - const »p1«: Int, - - // $TEST$ no info "Parameters of annotations implicitly have the const modifier." - »p2«: Int, -) - -fun f( - g: ( - // $TEST$ error "The const modifier is not applicable to parameters of callable types." - const »p1«: Int, - - // $TEST$ no error "The const modifier is not applicable to parameters of callable types." - »p2«: Int, - ) -> () -) - -pipeline myPipeline { - ( - // $TEST$ error "The const modifier is not applicable to parameters of lambdas." - const »p1«: Int, - - // $TEST$ no error "The const modifier is not applicable to parameters of lambdas." - »p2«: Int, - ) {}; - - ( - // $TEST$ error "The const modifier is not applicable to parameters of lambdas." - const »p1«: Int, - - // $TEST$ no error "The const modifier is not applicable to parameters of lambdas." - »p2«: Int, - ) -> 1; -} diff --git a/tests/resources/validation/other/declarations/parameters/const modifier/no error.sdstest b/tests/resources/validation/other/declarations/parameters/const modifier/no error.sdstest new file mode 100644 index 000000000..1f0601ead --- /dev/null +++ b/tests/resources/validation/other/declarations/parameters/const modifier/no error.sdstest @@ -0,0 +1,13 @@ +package tests.validation.other.declarations.parameters.illegalConstModifier + +// $TEST$ no error "The const modifier is not applicable to parameters of callable types." + +fun f( + g: (p2: Int) -> () +) + +pipeline myPipeline { + (p2: Int) {}; + + (p2: Int) -> 1; +} diff --git a/tests/resources/validation/style/unnecessary const modifier on annotation parameter/info.sdstest b/tests/resources/validation/style/unnecessary const modifier on annotation parameter/info.sdstest new file mode 100644 index 000000000..c23d31713 --- /dev/null +++ b/tests/resources/validation/style/unnecessary const modifier on annotation parameter/info.sdstest @@ -0,0 +1,6 @@ +package tests.validation.style.unnecessaryConstModifierOnAnnotationParameters + +annotation MyAnnotation( + // $TEST$ info "Annotation parameters are always const, so this modifier can be removed." + »const« p1: Int +) diff --git a/tests/resources/validation/style/unnecessary const modifier on annotation parameter/no info.sdstest b/tests/resources/validation/style/unnecessary const modifier on annotation parameter/no info.sdstest new file mode 100644 index 000000000..d9d5f58fd --- /dev/null +++ b/tests/resources/validation/style/unnecessary const modifier on annotation parameter/no info.sdstest @@ -0,0 +1,6 @@ +package tests.validation.style.unnecessaryConstModifierOnAnnotationParameters + +annotation MyAnnotation( + // $TEST$ no info "Annotation parameters are always const, so this modifier can be removed." + p2: Int, +)