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 dscanner.analysis.comma_expression;
7 
8 import dparse.ast;
9 import dparse.lexer;
10 import dscanner.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, bool skipTests = false)
21 	{
22 		super(fileName, sc, skipTests);
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 		if (ss.expression !is null)
45 		{
46 			++interest;
47 			visit(ss.expression);
48 			--interest;
49 		}
50 		visit(ss.statementNoCaseNoDefault);
51 	}
52 
53 	invariant
54 	{
55 		assert(interest >= 0);
56 	}
57 
58 	int interest;
59 
60 	private enum string KEY = "dscanner.suspicious.comma_expression";
61 }