Merge pull request #257 from Hackerpilot/issue-256

Issue 256
This commit is contained in:
Brian Schott 2015-05-17 17:14:49 -07:00
commit 327e629867
4 changed files with 72 additions and 1 deletions

View File

@ -11,6 +11,8 @@ SRC = src/*.d\
INCLUDE_PATHS = -Ilibdparse/src
VERSIONS =
DEBUG_VERSIONS = -version=std_parser_verbose
DMD_FLAGS = -w -O -release -inline
#DMD_FLAGS = -w
all: dmdbuild
ldc: ldcbuild
@ -24,7 +26,7 @@ debug:
dmdbuild: githash
mkdir -p bin
${DMD} -w -O -release -inline -ofbin/dscanner ${VERSIONS} ${INCLUDE_PATHS} ${SRC} -J.
${DMD} ${DMD_FLAGS} -ofbin/dscanner ${VERSIONS} ${INCLUDE_PATHS} ${SRC} -J.
rm -f bin/dscanner.o
gdcbuild: githash

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,64 @@
// 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;
if (unary.primaryExpression is null)
goto end;
if (unary.primaryExpression.expression is null)
goto end;
addErrorMessage(unary.primaryExpression.expression.line,
unary.primaryExpression.expression.column, KEY, "Redundant parenthesis");
end:
statement.accept(this);
}
override void visit(const PrimaryExpression primaryExpression)
{
import std.stdio : stderr;
UnaryExpression unary;
if (primaryExpression.expression is null)
goto end;
unary = cast(UnaryExpression) primaryExpression.expression.items[0];
if (unary is null)
goto end;
if (unary.primaryExpression is null)
goto end;
if (unary.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)