Issue warning on LastCatch as well

This commit is contained in:
Hackerpilot 2014-05-28 10:50:02 -07:00
parent f54f7823dc
commit f8c9f5c955
3 changed files with 20 additions and 4 deletions

View File

@ -25,6 +25,8 @@ import analysis.helpers;
*/ */
class PokemonExceptionCheck : BaseAnalyzer class PokemonExceptionCheck : BaseAnalyzer
{ {
enum message = "Catching Error or Throwable is almost always a bad idea";
alias visit = BaseAnalyzer.visit; alias visit = BaseAnalyzer.visit;
this(string fileName) this(string fileName)
@ -32,6 +34,12 @@ class PokemonExceptionCheck : BaseAnalyzer
super(fileName); super(fileName);
} }
override void visit(const LastCatch lc)
{
addErrorMessage(lc.line, lc.column, message);
lc.accept(this);
}
override void visit(const Catch c) override void visit(const Catch c)
{ {
if (c.type.type2.symbol.identifierOrTemplateChain.identifiersOrTemplateInstances.length != 1) if (c.type.type2.symbol.identifierOrTemplateChain.identifiersOrTemplateInstances.length != 1)
@ -50,7 +58,7 @@ class PokemonExceptionCheck : BaseAnalyzer
{ {
immutable column = identOrTemplate.identifier.column; immutable column = identOrTemplate.identifier.column;
immutable line = identOrTemplate.identifier.line; immutable line = identOrTemplate.identifier.line;
addErrorMessage(line, column, "Catching Error or Throwable is a really bad idea."); addErrorMessage(line, column, message);
} }
c.accept(this); c.accept(this);
} }
@ -73,14 +81,17 @@ unittest
{ {
} }
catch(Error err) // [warn]: Catching Error or Throwable is a really bad idea. catch(Error err) // [warn]: Catching Error or Throwable is almost always a bad idea
{ {
} }
catch(Throwable err) // [warn]: Catching Error or Throwable is a really bad idea. catch(Throwable err) // [warn]: Catching Error or Throwable is almost always a bad idea
{ {
} }
catch // [warn]: Catching Error or Throwable is almost always a bad idea
{
}
} }
}c, analysis.run.AnalyzerCheck.exception_check); }c, analysis.run.AnalyzerCheck.exception_check);

View File

@ -1871,6 +1871,8 @@ public:
mixin (visitIfNotNull!(statementNoCaseNoDefault)); mixin (visitIfNotNull!(statementNoCaseNoDefault));
} }
/** */ StatementNoCaseNoDefault statementNoCaseNoDefault; /** */ StatementNoCaseNoDefault statementNoCaseNoDefault;
size_t line;
size_t column;
mixin OpEquals; mixin OpEquals;
} }

View File

@ -3405,7 +3405,10 @@ invariant() foo();
{ {
mixin(traceEnterAndExit!(__FUNCTION__)); mixin(traceEnterAndExit!(__FUNCTION__));
auto node = allocate!LastCatch; auto node = allocate!LastCatch;
if (expect(tok!"catch") is null) return null; auto t = expect(tok!"catch");
if (t is null) return null;
node.line = t.line;
node.column = t.column;
if ((node.statementNoCaseNoDefault = parseStatementNoCaseNoDefault()) is null) if ((node.statementNoCaseNoDefault = parseStatementNoCaseNoDefault()) is null)
return null; return null;
return node; return node;