1 //          Copyright Brian Schott (Hackerpilot) 2015.
2 // Distributed under the Boost Software License, Version 1.0.
3 //    (See accompanying file LICENSE_1_0.txt or copy at
4 //          http://www.boost.org/LICENSE_1_0.txt)
5 
6 module dscanner.analysis.redundant_parens;
7 
8 import dparse.ast;
9 import dparse.lexer;
10 import dscanner.analysis.base;
11 import dsymbol.scope_ : Scope;
12 
13 /**
14  * Checks for redundant parenthesis
15  */
16 class RedundantParenCheck : BaseAnalyzer
17 {
18 	alias visit = BaseAnalyzer.visit;
19 
20 	///
21 	this(string fileName, const(Scope)* sc, bool skipTests = false)
22 	{
23 		super(fileName, sc, skipTests);
24 	}
25 
26 	override void visit(const IfStatement statement)
27 	{
28 		UnaryExpression unary;
29 		if (statement.expression is null || statement.expression.items.length != 1)
30 			goto end;
31 		unary = cast(UnaryExpression) statement.expression.items[0];
32 		if (unary is null)
33 			goto end;
34 		if (unary.primaryExpression is null)
35 			goto end;
36 		if (unary.primaryExpression.expression is null)
37 			goto end;
38 		addErrorMessage(unary.primaryExpression.expression.line,
39 				unary.primaryExpression.expression.column, KEY, "Redundant parenthesis.");
40 	end:
41 		statement.accept(this);
42 	}
43 
44 	override void visit(const PrimaryExpression primaryExpression)
45 	{
46 		UnaryExpression unary;
47 		if (primaryExpression.expression is null)
48 			goto end;
49 		unary = cast(UnaryExpression) primaryExpression.expression.items[0];
50 		if (unary is null)
51 			goto end;
52 		if (unary.primaryExpression is null)
53 			goto end;
54 		if (unary.primaryExpression.expression is null)
55 			goto end;
56 		addErrorMessage(primaryExpression.expression.line,
57 				primaryExpression.expression.column, KEY, "Redundant parenthesis.");
58 	end:
59 		primaryExpression.accept(this);
60 	}
61 
62 private:
63 	enum string KEY = "dscanner.suspicious.redundant_parens";
64 }