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 final class RedundantParenCheck : BaseAnalyzer
17 {
18 	alias visit = BaseAnalyzer.visit;
19 
20 	mixin AnalyzerInfo!"redundant_parens_check";
21 
22 	///
23 	this(string fileName, const(Scope)* sc, bool skipTests = false)
24 	{
25 		super(fileName, sc, skipTests);
26 	}
27 
28 	override void visit(const IfStatement statement)
29 	{
30 		UnaryExpression unary;
31 		if (statement.expression is null || statement.expression.items.length != 1)
32 			goto end;
33 		unary = cast(UnaryExpression) statement.expression.items[0];
34 		if (unary is null)
35 			goto end;
36 		if (unary.primaryExpression is null)
37 			goto end;
38 		if (unary.primaryExpression.expression is null)
39 			goto end;
40 		addErrorMessage(unary.primaryExpression.expression.line,
41 				unary.primaryExpression.expression.column, KEY, "Redundant parenthesis.");
42 	end:
43 		statement.accept(this);
44 	}
45 
46 	override void visit(const PrimaryExpression primaryExpression)
47 	{
48 		UnaryExpression unary;
49 		if (primaryExpression.expression is null)
50 			goto end;
51 		unary = cast(UnaryExpression) primaryExpression.expression.items[0];
52 		if (unary is null)
53 			goto end;
54 		if (unary.primaryExpression is null)
55 			goto end;
56 		if (unary.primaryExpression.expression is null)
57 			goto end;
58 		addErrorMessage(primaryExpression.expression.line,
59 				primaryExpression.expression.column, KEY, "Redundant parenthesis.");
60 	end:
61 		primaryExpression.accept(this);
62 	}
63 
64 private:
65 	enum string KEY = "dscanner.suspicious.redundant_parens";
66 }