/
redundant_parens.d
60 lines (53 loc) · 1.62 KB
/
redundant_parens.d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// 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 dparse.ast;
import dparse.lexer;
import analysis.base;
import dsymbol.scope_ : Scope;
class RedundantParenCheck : BaseAnalyzer
{
alias visit = BaseAnalyzer.visit;
this(string fileName, const(Scope)* sc)
{
super(fileName, sc);
}
override void visit(const IfStatement statement)
{
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)
{
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";
}