Added check for identical if/else blocks and obvious self-assignment
This commit is contained in:
parent
06cf70b34c
commit
7c22e9a849
|
@ -0,0 +1,43 @@
|
||||||
|
// Copyright Brian Schott (Sir Alaran) 2014.
|
||||||
|
// 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.ifelsesame;
|
||||||
|
|
||||||
|
import stdx.d.ast;
|
||||||
|
import stdx.d.lexer;
|
||||||
|
import analysis.base;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks for if statements whose "then" block is the same as the "else" block
|
||||||
|
*/
|
||||||
|
class IfElseSameCheck : BaseAnalyzer
|
||||||
|
{
|
||||||
|
alias visit = BaseAnalyzer.visit;
|
||||||
|
|
||||||
|
this(string fileName)
|
||||||
|
{
|
||||||
|
super(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
override void visit(const IfStatement ifStatement)
|
||||||
|
{
|
||||||
|
if (ifStatement.thenStatement == ifStatement.elseStatement)
|
||||||
|
addErrorMessage(ifStatement.line, ifStatement.column,
|
||||||
|
"\"Else\" branch is identical to \"Then\" branch.");
|
||||||
|
ifStatement.accept(this);
|
||||||
|
}
|
||||||
|
|
||||||
|
override void visit(const AssignExpression assignExpression)
|
||||||
|
{
|
||||||
|
const AssignExpression e = cast(const AssignExpression) assignExpression.assignExpression;
|
||||||
|
if (e !is null && assignExpression.operator == tok!"="
|
||||||
|
&& e.ternaryExpression == assignExpression.ternaryExpression)
|
||||||
|
{
|
||||||
|
addErrorMessage(assignExpression.line, assignExpression.column,
|
||||||
|
"Left side of assignment operatior is identical to the right side");
|
||||||
|
}
|
||||||
|
assignExpression.accept(this);
|
||||||
|
}
|
||||||
|
}
|
|
@ -20,6 +20,7 @@ import analysis.fish;
|
||||||
import analysis.numbers;
|
import analysis.numbers;
|
||||||
import analysis.objectconst;
|
import analysis.objectconst;
|
||||||
import analysis.range;
|
import analysis.range;
|
||||||
|
import analysis.ifelsesame;
|
||||||
|
|
||||||
void messageFunction(string fileName, size_t line, size_t column, string message,
|
void messageFunction(string fileName, size_t line, size_t column, string message,
|
||||||
bool isError)
|
bool isError)
|
||||||
|
@ -70,6 +71,7 @@ void analyze(File output, string[] fileNames, bool staticAnalyze = true)
|
||||||
checks ~= new NumberStyleCheck(fileName);
|
checks ~= new NumberStyleCheck(fileName);
|
||||||
checks ~= new ObjectConstCheck(fileName);
|
checks ~= new ObjectConstCheck(fileName);
|
||||||
checks ~= new BackwardsRangeCheck(fileName);
|
checks ~= new BackwardsRangeCheck(fileName);
|
||||||
|
checks ~= new IfElseSameCheck(fileName);
|
||||||
|
|
||||||
foreach (check; checks)
|
foreach (check; checks)
|
||||||
{
|
{
|
||||||
|
|
237
stdx/d/ast.d
237
stdx/d/ast.d
File diff suppressed because it is too large
Load Diff
|
@ -557,7 +557,7 @@ public struct DLexer
|
||||||
Token lexNumber() pure nothrow
|
Token lexNumber() pure nothrow
|
||||||
{
|
{
|
||||||
mixin (tokenStart);
|
mixin (tokenStart);
|
||||||
if (range.canPeek(1) && range.front == '0')
|
if (range.front == '0' && range.canPeek(1))
|
||||||
{
|
{
|
||||||
auto ahead = range.peek(1)[1];
|
auto ahead = range.peek(1)[1];
|
||||||
switch (ahead)
|
switch (ahead)
|
||||||
|
|
|
@ -780,6 +780,8 @@ alias core.sys.posix.stdio.fileno fileno;
|
||||||
{
|
{
|
||||||
mixin(traceEnterAndExit!(__FUNCTION__));
|
mixin(traceEnterAndExit!(__FUNCTION__));
|
||||||
auto node = allocate!AssignExpression;
|
auto node = allocate!AssignExpression;
|
||||||
|
node.line = current().line;
|
||||||
|
node.column = current().column;
|
||||||
node.ternaryExpression = parseTernaryExpression();
|
node.ternaryExpression = parseTernaryExpression();
|
||||||
if (currentIsOneOf(tok!"=", tok!">>>=",
|
if (currentIsOneOf(tok!"=", tok!">>>=",
|
||||||
tok!">>=", tok!"<<=",
|
tok!">>=", tok!"<<=",
|
||||||
|
@ -2872,6 +2874,8 @@ body {} // six
|
||||||
{
|
{
|
||||||
mixin(traceEnterAndExit!(__FUNCTION__));
|
mixin(traceEnterAndExit!(__FUNCTION__));
|
||||||
auto node = allocate!IfStatement;
|
auto node = allocate!IfStatement;
|
||||||
|
node.line = current().line;
|
||||||
|
node.column = current().column;
|
||||||
if (expect(tok!"if") is null) return null;
|
if (expect(tok!"if") is null) return null;
|
||||||
node.startIndex = current().index;
|
node.startIndex = current().index;
|
||||||
if (expect(tok!"(") is null) return null;
|
if (expect(tok!"(") is null) return null;
|
||||||
|
|
Loading…
Reference in New Issue