1 //          Copyright Brian Schott (Hackerpilot) 2014.
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 analysis.comma_expression;
7 
8 import dparse.ast;
9 import dparse.lexer;
10 import analysis.base;
11 import dsymbol.scope_;
12 
13 /**
14  * Check for uses of the comma expression.
15  */
16 class CommaExpressionCheck : BaseAnalyzer
17 {
18 	alias visit = BaseAnalyzer.visit;
19 
20 	this(string fileName, const(Scope)* sc)
21 	{
22 		super(fileName, sc);
23 	}
24 
25 	override void visit(const Expression ex)
26 	{
27 		if (ex.items.length > 1 && interest > 0)
28 		{
29 			addErrorMessage(ex.line, ex.column, KEY, "Avoid using the comma expression.");
30 		}
31 		ex.accept(this);
32 	}
33 
34 	override void visit(const AssignExpression ex)
35 	{
36 		++interest;
37 		ex.accept(this);
38 		--interest;
39 	}
40 
41 	// Dconf 2016
42 	override void visit(const SynchronizedStatement ss)
43 	{
44 		++interest;
45 		visit(ss.expression);
46 		--interest;
47 		visit(ss.statementNoCaseNoDefault);
48 	}
49 
50 	invariant
51 	{
52 		assert(interest >= 0);
53 	}
54 
55 	int interest;
56 
57 	private enum string KEY = "dscanner.suspicious.comma_expression";
58 }