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 std.d.ast;
10 import std.d.lexer;
11 import analysis.base;
12 import analysis.helpers;
13 
14 /**
15  * Checks for use of the deprecated floating point comparison operators.
16  */
17 class FloatOperatorCheck : BaseAnalyzer
18 {
19 	alias visit = BaseAnalyzer.visit;
20 
21 	enum string KEY = "dscanner.deprecated.floating_point_operators";
22 
23 	this(string fileName)
24 	{
25 		super(fileName);
26 	}
27 
28 	override void visit(const RelExpression r)
29 	{
30 		if (r.operator == tok!"<>"
31 			|| 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 		{
39 			addErrorMessage(r.line, r.column, KEY,
40 				"Avoid using the deprecated floating-point operators.");
41 		}
42 		r.accept(this);
43 	}
44 }
45 
46 unittest
47 {
48 	import analysis.config : StaticAnalysisConfig;
49 
50 	StaticAnalysisConfig sac;
51 	sac.float_operator_check = true;
52 	assertAnalyzerWarnings(q{
53 		void testFish()
54 		{
55 			float z = 1.5f;
56 			bool a;
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 			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 		}
66 	}}, sac);
67 
68 	stderr.writeln("Unittest for FloatOperatorCheck passed.");
69 }
70