replace libdparse in exception check (#68)

This commit is contained in:
lucica28 2023-05-25 10:47:30 +03:00 committed by Vladiwostok
parent 6a832f4411
commit e88ba52754
2 changed files with 29 additions and 73 deletions

View file

@ -6,11 +6,8 @@
module dscanner.analysis.pokemon; module dscanner.analysis.pokemon;
import std.stdio; import std.stdio;
import dparse.ast;
import dparse.lexer;
import dscanner.analysis.base; import dscanner.analysis.base;
import dscanner.analysis.helpers; import dscanner.analysis.helpers;
import dsymbol.scope_ : Scope;
/** /**
* Checks for Pokémon exception handling, i.e. "gotta' catch 'em all". * Checks for Pokémon exception handling, i.e. "gotta' catch 'em all".
@ -23,62 +20,27 @@ import dsymbol.scope_ : Scope;
* } * }
* --- * ---
*/ */
final class PokemonExceptionCheck : BaseAnalyzer extern(C++) class PokemonExceptionCheck(AST) : BaseAnalyzerDmd
{ {
mixin AnalyzerInfo!"exception_check";
alias visit = BaseAnalyzerDmd.visit;
extern(D) this(string fileName, bool skipTests = false)
{
super(fileName, skipTests);
}
override void visit(AST.Catch c)
{
if (c.type.isTypeIdentifier().ident.toString() == "Error" ||
c.type.isTypeIdentifier().ident.toString() == "Throwable")
addErrorMessage(cast(ulong) c.loc.linnum, cast(ulong) c.loc.charnum,
KEY, MESSAGE);
}
private:
enum MESSAGE = "Catching Error or Throwable is almost always a bad idea."; enum MESSAGE = "Catching Error or Throwable is almost always a bad idea.";
enum string KEY = "dscanner.suspicious.catch_em_all"; enum string KEY = "dscanner.suspicious.catch_em_all";
mixin AnalyzerInfo!"exception_check";
alias visit = BaseAnalyzer.visit;
this(BaseAnalyzerArguments args)
{
super(args);
}
override void visit(const LastCatch lc)
{
addErrorMessage(lc.tokens[0], KEY, MESSAGE);
lc.accept(this);
}
bool ignoreType = true;
override void visit(const Catch c)
{
ignoreType = false;
c.type.accept(this);
ignoreType = true;
c.accept(this);
}
override void visit(const Type2 type2)
{
if (ignoreType)
return;
if (type2.type !is null)
{
type2.type.accept(this);
return;
}
if (type2.typeIdentifierPart.typeIdentifierPart !is null)
{
return;
}
const identOrTemplate = type2.typeIdentifierPart.identifierOrTemplateInstance;
if (identOrTemplate.templateInstance !is null)
{
return;
}
if (identOrTemplate.identifier.text == "Throwable"
|| identOrTemplate.identifier.text == "Error")
{
addErrorMessage(identOrTemplate, KEY, MESSAGE);
}
}
} }
unittest unittest
@ -87,7 +49,7 @@ unittest
StaticAnalysisConfig sac = disabledConfig(); StaticAnalysisConfig sac = disabledConfig();
sac.exception_check = Check.enabled; sac.exception_check = Check.enabled;
assertAnalyzerWarnings(q{ assertAnalyzerWarningsDMD(q{
void testCatch() void testCatch()
{ {
try try
@ -106,23 +68,15 @@ unittest
{ {
} }
catch (Error err) /+ catch (Error err) // [warn]: Catching Error or Throwable is almost always a bad idea.
^^^^^ [warn]: Catching Error or Throwable is almost always a bad idea. +/
{ {
} }
catch (Throwable err) /+ catch (Throwable err) // [warn]: Catching Error or Throwable is almost always a bad idea.
^^^^^^^^^ [warn]: Catching Error or Throwable is almost always a bad idea. +/
{ {
} }
catch (shared(Error) err) /+ catch (shared(Error) err) // [warn]: Catching Error or Throwable is almost always a bad idea.
^^^^^ [warn]: Catching Error or Throwable is almost always a bad idea. +/
{
}
catch /+
^^^^^ [warn]: Catching Error or Throwable is almost always a bad idea. +/
{ {
} }
@ -130,4 +84,4 @@ unittest
}c, sac); }c, sac);
stderr.writeln("Unittest for PokemonExceptionCheck passed."); stderr.writeln("Unittest for PokemonExceptionCheck passed.");
} }

View file

@ -853,10 +853,6 @@ private BaseAnalyzer[] getAnalyzersForModuleAndConfig(string fileName,
checks ~= new DuplicateAttributeCheck(args.setSkipTests( checks ~= new DuplicateAttributeCheck(args.setSkipTests(
analysisConfig.duplicate_attribute == Check.skipTests && !ut)); analysisConfig.duplicate_attribute == Check.skipTests && !ut));
if (moduleName.shouldRun!PokemonExceptionCheck(analysisConfig))
checks ~= new PokemonExceptionCheck(args.setSkipTests(
analysisConfig.exception_check == Check.skipTests && !ut));
if (moduleName.shouldRun!FloatOperatorCheck(analysisConfig)) if (moduleName.shouldRun!FloatOperatorCheck(analysisConfig))
checks ~= new FloatOperatorCheck(args.setSkipTests( checks ~= new FloatOperatorCheck(args.setSkipTests(
analysisConfig.float_operator_check == Check.skipTests && !ut)); analysisConfig.float_operator_check == Check.skipTests && !ut));
@ -1319,6 +1315,12 @@ MessageSet analyzeDmd(string fileName, ASTCodegen.Module m, const char[] moduleN
if (moduleName.shouldRunDmd!(BuiltinPropertyNameCheck!ASTCodegen)(config)) if (moduleName.shouldRunDmd!(BuiltinPropertyNameCheck!ASTCodegen)(config))
visitors ~= new BuiltinPropertyNameCheck!ASTCodegen(fileName); visitors ~= new BuiltinPropertyNameCheck!ASTCodegen(fileName);
if (moduleName.shouldRunDmd!(PokemonExceptionCheck!ASTCodegen)(config))
visitors ~= new PokemonExceptionCheck!ASTCodegen(
fileName,
config.exception_check == Check.skipTests && !ut
);
if (moduleName.shouldRunDmd!(BackwardsRangeCheck!ASTCodegen)(config)) if (moduleName.shouldRunDmd!(BackwardsRangeCheck!ASTCodegen)(config))
visitors ~= new BackwardsRangeCheck!ASTCodegen( visitors ~= new BackwardsRangeCheck!ASTCodegen(
fileName, fileName,