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 final class FloatOperatorCheck : BaseAnalyzer 19 { 20 alias visit = BaseAnalyzer.visit; 21 22 enum string KEY = "dscanner.deprecated.floating_point_operators"; 23 mixin AnalyzerInfo!"float_operator_check"; 24 25 this(string fileName, const(Scope)* sc, bool skipTests = false) 26 { 27 super(fileName, sc, skipTests); 28 } 29 30 override void visit(const RelExpression r) 31 { 32 if (r.operator == tok!"<>" || r.operator == tok!"<>=" 33 || r.operator == tok!"!<>" || r.operator == tok!"!>" 34 || r.operator == tok!"!<" || r.operator == tok!"!<>=" 35 || r.operator == tok!"!>=" || r.operator == tok!"!<=") 36 { 37 addErrorMessage(r.line, r.column, KEY, 38 "Avoid using the deprecated floating-point operators."); 39 } 40 r.accept(this); 41 } 42 } 43 44 unittest 45 { 46 import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig; 47 48 StaticAnalysisConfig sac = disabledConfig(); 49 sac.float_operator_check = Check.enabled; 50 assertAnalyzerWarnings(q{ 51 void testFish() 52 { 53 float z = 1.5f; 54 bool a; 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 a = z !<= z; // [warn]: Avoid using the deprecated floating-point operators. 63 } 64 }}, sac); 65 66 stderr.writeln("Unittest for FloatOperatorCheck passed."); 67 }