This commit is contained in:
Hackerpilot 2015-05-17 16:22:51 -07:00
parent fd4b4edda9
commit 960881e607
3 changed files with 56 additions and 0 deletions

View File

@ -89,4 +89,7 @@ struct StaticAnalysisConfig
@INI("Checks for redundant expressions in if statements")
bool redundant_if_check;
@INI("Checks for redundant parenthesis")
bool redundant_parens_check;
}

View File

@ -0,0 +1,51 @@
// Copyright Brian Schott (Hackerpilot) 2015.
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
module analysis.redundant_parens;
import std.d.ast;
import std.d.lexer;
import analysis.base;
class RedundantParenCheck : BaseAnalyzer
{
alias visit = BaseAnalyzer.visit;
this(string fileName)
{
super(fileName);
}
override void visit(const IfStatement statement)
{
import std.stdio : stderr;
stderr.writeln(__PRETTY_FUNCTION__);
UnaryExpression unary;
if (statement.expression is null || statement.expression.items.length != 1)
goto end;
unary = cast(UnaryExpression) statement.expression.items[0];
if (unary is null)
goto end;
visit(unary.primaryExpression);
end:
statement.accept(this);
}
override void visit(const PrimaryExpression primaryExpression)
{
if (primaryExpression is null)
goto end;
if (primaryExpression.expression is null)
goto end;
addErrorMessage(primaryExpression.expression.line,
primaryExpression.expression.column, KEY, "Redundant parenthesis");
end:
primaryExpression.accept(this);
}
private:
enum KEY = "dscanner.suspicious.redundant_parens";
}

View File

@ -42,6 +42,7 @@ import analysis.function_attributes;
import analysis.local_imports;
import analysis.unmodified;
import analysis.if_statements;
import analysis.redundant_parens;
bool first = true;
@ -190,6 +191,7 @@ MessageSet analyze(string fileName, const Module m,
if (analysisConfig.comma_expression_check) checks ~= new CommaExpressionCheck(fileName);
if (analysisConfig.local_import_check) checks ~= new LocalImportCheck(fileName);
if (analysisConfig.could_be_immutable_check) checks ~= new UnmodifiedFinder(fileName);
if (analysisConfig.redundant_parens_check) checks ~= new RedundantParenCheck(fileName);
version(none) if (analysisConfig.redundant_if_check) checks ~= new IfStatementCheck(fileName);
foreach (check; checks)