implement new ThrowExpression & shortened methods

This commit is contained in:
WebFreak001 2022-01-31 18:04:17 +01:00
parent 873c0f2156
commit a3fe7611a8
4 changed files with 30 additions and 18 deletions

View File

@ -46,6 +46,8 @@ final class IncorrectInfiniteRangeCheck : BaseAnalyzer
{ {
if (fb.specifiedFunctionBody && fb.specifiedFunctionBody.blockStatement !is null) if (fb.specifiedFunctionBody && fb.specifiedFunctionBody.blockStatement !is null)
visit(fb.specifiedFunctionBody.blockStatement); visit(fb.specifiedFunctionBody.blockStatement);
else if (fb.shortenedFunctionBody && fb.shortenedFunctionBody.expression !is null)
visitReturnExpression(fb.shortenedFunctionBody.expression);
} }
override void visit(const BlockStatement bs) override void visit(const BlockStatement bs)
@ -63,9 +65,14 @@ final class IncorrectInfiniteRangeCheck : BaseAnalyzer
{ {
if (inStruct == 0 || line == size_t.max) // not within a struct yet if (inStruct == 0 || line == size_t.max) // not within a struct yet
return; return;
if (!rs.expression || rs.expression.items.length != 1) visitReturnExpression(rs.expression);
}
void visitReturnExpression(const Expression expression)
{
if (!expression || expression.items.length != 1)
return; return;
UnaryExpression unary = cast(UnaryExpression) rs.expression.items[0]; UnaryExpression unary = cast(UnaryExpression) expression.items[0];
if (unary is null) if (unary is null)
return; return;
if (unary.primaryExpression is null) if (unary.primaryExpression is null)

View File

@ -55,6 +55,8 @@ final class ProperlyDocumentedPublicFunctions : BaseAnalyzer
override void visit(const UnaryExpression decl) override void visit(const UnaryExpression decl)
{ {
import std.algorithm.searching : canFind;
const IdentifierOrTemplateInstance iot = safeAccess(decl) const IdentifierOrTemplateInstance iot = safeAccess(decl)
.functionCallExpression.unaryExpression.primaryExpression .functionCallExpression.unaryExpression.primaryExpression
.identifierOrTemplateInstance; .identifierOrTemplateInstance;
@ -69,6 +71,12 @@ final class ProperlyDocumentedPublicFunctions : BaseAnalyzer
return t; return t;
} }
if (inThrowExpression && decl.newExpression && decl.newExpression.type &&
!thrown.canFind!(a => a == decl.newExpression.type))
{
thrown ~= decl.newExpression.type;
}
// enforce(condition); // enforce(condition);
if (iot && iot.identifier.text == "enforce") if (iot && iot.identifier.text == "enforce")
{ {
@ -187,7 +195,7 @@ final class ProperlyDocumentedPublicFunctions : BaseAnalyzer
if (nestedFuncs == 1) if (nestedFuncs == 1)
thrown.length = 0; thrown.length = 0;
// detect ThrowStatement only if not nothrow // detect ThrowExpression only if not nothrow
if (!decl.attributes.any!(a => a.attribute.text == "nothrow")) if (!decl.attributes.any!(a => a.attribute.text == "nothrow"))
{ {
decl.accept(this); decl.accept(this);
@ -228,20 +236,14 @@ final class ProperlyDocumentedPublicFunctions : BaseAnalyzer
.array; .array;
} }
override void visit(const ThrowStatement ts) override void visit(const ThrowExpression ts)
{ {
import std.algorithm.searching : canFind; const wasInThrowExpression = inThrowExpression;
inThrowExpression = true;
scope (exit)
inThrowExpression = wasInThrowExpression;
ts.accept(this); ts.accept(this);
if (ts.expression && ts.expression.items.length == 1) inThrowExpression = false;
if (const UnaryExpression ue = cast(UnaryExpression) ts.expression.items[0])
{
if (ue.newExpression && ue.newExpression.type &&
!thrown.canFind!(a => a == ue.newExpression.type))
{
thrown ~= ue.newExpression.type;
}
}
} }
alias visit = BaseAnalyzer.visit; alias visit = BaseAnalyzer.visit;
@ -260,6 +262,7 @@ private:
} }
Function lastSeenFun; Function lastSeenFun;
bool inThrowExpression;
const(Type)[] thrown; const(Type)[] thrown;
// find invalid ddoc parameters (i.e. they don't occur in a function declaration) // find invalid ddoc parameters (i.e. they don't occur in a function declaration)

View File

@ -48,7 +48,9 @@ abstract class UnusedIdentifierCheck : BaseAnalyzer
override void visit(const FunctionDeclaration functionDec) override void visit(const FunctionDeclaration functionDec)
{ {
pushScope(); pushScope();
if (functionDec.functionBody && functionDec.functionBody.specifiedFunctionBody) if (functionDec.functionBody
&& (functionDec.functionBody.specifiedFunctionBody
|| functionDec.functionBody.shortenedFunctionBody))
{ {
immutable bool ias = inAggregateScope; immutable bool ias = inAggregateScope;
inAggregateScope = false; inAggregateScope = false;
@ -74,7 +76,7 @@ abstract class UnusedIdentifierCheck : BaseAnalyzer
mixin PartsUseVariables!StaticIfCondition; mixin PartsUseVariables!StaticIfCondition;
mixin PartsUseVariables!StructDeclaration; mixin PartsUseVariables!StructDeclaration;
mixin PartsUseVariables!TemplateArgumentList; mixin PartsUseVariables!TemplateArgumentList;
mixin PartsUseVariables!ThrowStatement; mixin PartsUseVariables!ThrowExpression;
mixin PartsUseVariables!CastExpression; mixin PartsUseVariables!CastExpression;
override void visit(const SwitchStatement switchStatement) override void visit(const SwitchStatement switchStatement)

View File

@ -1146,7 +1146,7 @@ class XMLPrinter : ASTVisitor
override void visit(const TemplateValueParameter templateValueParameter) { mixin (tagAndAccept!"templateValueParameter"); } override void visit(const TemplateValueParameter templateValueParameter) { mixin (tagAndAccept!"templateValueParameter"); }
override void visit(const TernaryExpression ternaryExpression) { mixin (tagAndAccept!"ternaryExpression"); } override void visit(const TernaryExpression ternaryExpression) { mixin (tagAndAccept!"ternaryExpression"); }
override void visit(const TypeIdentifierPart typeIdentifierPart) { mixin (tagAndAccept!"typeIdentifierPart"); } override void visit(const TypeIdentifierPart typeIdentifierPart) { mixin (tagAndAccept!"typeIdentifierPart"); }
override void visit(const ThrowStatement throwStatement) { mixin (tagAndAccept!"throwStatement"); } override void visit(const ThrowExpression throwExpression) { mixin (tagAndAccept!"throwExpression"); }
override void visit(const TryStatement tryStatement) { mixin (tagAndAccept!"tryStatement"); } override void visit(const TemplateInstance templateInstance) { mixin (tagAndAccept!"templateInstance"); } override void visit(const TryStatement tryStatement) { mixin (tagAndAccept!"tryStatement"); } override void visit(const TemplateInstance templateInstance) { mixin (tagAndAccept!"templateInstance"); }
override void visit(const TypeofExpression typeofExpression) { mixin (tagAndAccept!"typeofExpression"); } override void visit(const TypeSpecialization typeSpecialization) { mixin (tagAndAccept!"typeSpecialization"); } override void visit(const TraitsExpression traitsExpression) { mixin (tagAndAccept!"traitsExpression"); } override void visit(const TypeofExpression typeofExpression) { mixin (tagAndAccept!"typeofExpression"); } override void visit(const TypeSpecialization typeSpecialization) { mixin (tagAndAccept!"typeSpecialization"); } override void visit(const TraitsExpression traitsExpression) { mixin (tagAndAccept!"traitsExpression"); }
override void visit(const Vector vector) { mixin (tagAndAccept!"vector"); } override void visit(const Vector vector) { mixin (tagAndAccept!"vector"); }