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.fish;
7 
8 import std.stdio;
9 import dparse.ast;
10 import dparse.lexer;
11 import dscanner.analysis.base;
12 import dscanner.analysis.helpers;
13 import dsymbol.scope_ : Scope;
14 
15 /**
16  * Checks for use of the deprecated floating point comparison operators.
17  */
18 class FloatOperatorCheck : BaseAnalyzer
19 {
20 	alias visit = BaseAnalyzer.visit;
21 
22 	enum string KEY = "dscanner.deprecated.floating_point_operators";
23 
24 	this(string fileName, const(Scope)* sc, bool skipTests = false)
25 	{
26 		super(fileName, sc, skipTests);
27 	}
28 
29 	override void visit(const RelExpression r)
30 	{
31 		if (r.operator == tok!"<>" || r.operator == tok!"<>="
32 				|| r.operator == tok!"!<>" || r.operator == tok!"!>"
33 				|| r.operator == tok!"!<" || r.operator == tok!"!<>="
34 				|| r.operator == tok!"!>=" || r.operator == tok!"!<=")
35 		{
36 			addErrorMessage(r.line, r.column, KEY,
37 					"Avoid using the deprecated floating-point operators.");
38 		}
39 		r.accept(this);
40 	}
41 }
42 
43 unittest
44 {
45 	import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
46 
47 	StaticAnalysisConfig sac = disabledConfig();
48 	sac.float_operator_check = Check.enabled;
49 	assertAnalyzerWarnings(q{
50 		void testFish()
51 		{
52 			float z = 1.5f;
53 			bool a;
54 			a = z !<>= z; // [warn]: Avoid using the deprecated floating-point operators.
55 			a = z !<> z; // [warn]: Avoid using the deprecated floating-point operators.
56 			a = z <> z; // [warn]: Avoid using the deprecated floating-point operators.
57 			a = z <>= z; // [warn]: Avoid using the deprecated floating-point operators.
58 			a = z !> z; // [warn]: Avoid using the deprecated floating-point operators.
59 			a = z !>= z; // [warn]: Avoid using the deprecated floating-point operators.
60 			a = z !< z; // [warn]: Avoid using the deprecated floating-point operators.
61 			a = z !<= z; // [warn]: Avoid using the deprecated floating-point operators.
62 		}
63 	}c, sac);
64 
65 	stderr.writeln("Unittest for FloatOperatorCheck passed.");
66 }