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.fish;
7 
8 import std.stdio;
9 import dparse.ast;
10 import dparse.lexer;
11 import analysis.base;
12 import 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)
25 	{
26 		super(fileName, sc);
27 	}
28 
29 	override void visit(const RelExpression r)
30 	{
31 		if (r.operator == tok!"<>"
32 			|| r.operator == tok!"<>="
33 			|| r.operator == tok!"!<>"
34 			|| r.operator == tok!"!>"
35 			|| r.operator == tok!"!<"
36 			|| r.operator == tok!"!<>="
37 			|| r.operator == tok!"!>="
38 			|| r.operator == tok!"!<=")
39 		{
40 			addErrorMessage(r.line, r.column, KEY,
41 				"Avoid using the deprecated floating-point operators.");
42 		}
43 		r.accept(this);
44 	}
45 }
46 
47 unittest
48 {
49 	import analysis.config : StaticAnalysisConfig;
50 
51 	StaticAnalysisConfig sac;
52 	sac.float_operator_check = true;
53 	assertAnalyzerWarnings(q{
54 		void testFish()
55 		{
56 			float z = 1.5f;
57 			bool a;
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 			a = z !> z; // [warn]: Avoid using the deprecated floating-point operators.
63 			a = z !>= z; // [warn]: Avoid using the deprecated floating-point operators.
64 			a = z !< z; // [warn]: Avoid using the deprecated floating-point operators.
65 			a = z !<= z; // [warn]: Avoid using the deprecated floating-point operators.
66 		}
67 	}}, sac);
68 
69 	stderr.writeln("Unittest for FloatOperatorCheck passed.");
70 }
71