Implement check for logical or and logical and operands

This commit is contained in:
Hackerpilot 2014-08-13 14:37:35 -07:00
parent 105f2680e4
commit f2d121b71f
4 changed files with 38 additions and 10 deletions

View File

@ -30,13 +30,13 @@ source file.
std.stdio std.stdio
### Syntax Check ### Syntax Check
The "--syntaxCheck" option prints a listing of any errors or warnings found The "--syntaxCheck" or "-s" option prints a listing of any errors or warnings found
while lexing or parsing the given source file. It does not do any semantic while lexing or parsing the given source file. It does not do any semantic
analysis and it does not compile the code. analysis and it does not compile the code.
### Style Check ### Style Check
The "--styleCheck" option runs some basic static analysis checks against the The "--styleCheck" or "-S" option runs some basic static analysis checks against
given source files. the given source files.
#### Implemented checks #### Implemented checks
* Old alias syntax (i.e "alias a b;" should be replaced with "alias b = a;"). * Old alias syntax (i.e "alias a b;" should be replaced with "alias b = a;").
@ -55,6 +55,7 @@ given source files.
* Struct constructors that have a single parameter that has a default argument. * Struct constructors that have a single parameter that has a default argument.
* Assign expressions where the left side of the '=' operator is the same as the right * Assign expressions where the left side of the '=' operator is the same as the right
* 'if' statements where the 'else' block is the same as the 'if' block. * 'if' statements where the 'else' block is the same as the 'if' block.
* ||, &&, and == expressions where the left and right sides of the operator are identical
* Unused variables. * Unused variables.
* Unused parameters (check is skipped if function is marked "override") * Unused parameters (check is skipped if function is marked "override")
* Duplicate attributes * Duplicate attributes

View File

@ -14,7 +14,12 @@ import analysis.helpers;
/** /**
* Checks for if statements whose "then" block is the same as the "else" block * Checks for duplicated code in conditional and logical expressions.
* $(UL
* $(LI If statements whose "then" block is the same as the "else" block)
* $(LI || and && expressions where the left and right are the same)
* $(LI == expressions where the left and right are the same)
* )
*/ */
class IfElseSameCheck : BaseAnalyzer class IfElseSameCheck : BaseAnalyzer
{ {
@ -44,6 +49,28 @@ class IfElseSameCheck : BaseAnalyzer
} }
assignExpression.accept(this); assignExpression.accept(this);
} }
override void visit(const AndAndExpression andAndExpression)
{
if (andAndExpression.left !is null && andAndExpression.right !is null
&& andAndExpression.left == andAndExpression.right)
{
addErrorMessage(andAndExpression.line, andAndExpression.column,
"Left side of logical and is identical to right side.");
}
andAndExpression.accept(this);
}
override void visit(const OrOrExpression orOrExpression)
{
if (orOrExpression.left !is null && orOrExpression.right !is null
&& orOrExpression.left == orOrExpression.right)
{
addErrorMessage(orOrExpression.line, orOrExpression.column,
"Left side of logical or is identical to right side.");
}
orOrExpression.accept(this);
}
} }
unittest unittest

@ -1 +1 @@
Subproject commit 9187ad6c775e91c78fd2e9313d2d66d1922445eb Subproject commit acb6b43655b1144fbcaf08ec0442a81143cdb569

10
main.d
View File

@ -59,11 +59,11 @@ int run(string[] args)
try try
{ {
getopt(args, "sloc|l", &sloc, "highlight", &highlight, getopt(args, std.getopt.config.caseSensitive, "sloc|l", &sloc,
"ctags|c", &ctags, "recursive|r|R", &recursive, "help|h", &help, "highlight", &highlight, "ctags|c", &ctags, "recursive|r|R", &recursive,
"tokenCount|t", &tokenCount, "syntaxCheck|s", &syntaxCheck, "help|h", &help, "tokenCount|t", &tokenCount, "syntaxCheck|s", &syntaxCheck,
"ast|xml", &ast, "imports|i", &imports, "outline|o", &outline, "ast|xml", &ast, "imports|i", &imports, "outline|o", &outline,
"tokenDump", &tokenDump, "styleCheck", &styleCheck, "tokenDump", &tokenDump, "styleCheck|S", &styleCheck,
"defaultConfig", &defaultConfig, "muffinButton", &muffin); "defaultConfig", &defaultConfig, "muffinButton", &muffin);
} }
catch (ConvException e) catch (ConvException e)
@ -319,7 +319,7 @@ options:
syntax errors to stdout. One error or warning is printed per line. syntax errors to stdout. One error or warning is printed per line.
If no files are specified, input is read from stdin. If no files are specified, input is read from stdin.
--styleCheck [sourceFiles] --styleCheck | -S [sourceFiles]
Lexes and parses sourceFiles, printing the line and column number of any Lexes and parses sourceFiles, printing the line and column number of any
static analysis check failures stdout. static analysis check failures stdout.