diff --git a/src/dscanner/analysis/ifelsesame.d b/src/dscanner/analysis/ifelsesame.d index d765b521..076a207b 100644 --- a/src/dscanner/analysis/ifelsesame.d +++ b/src/dscanner/analysis/ifelsesame.d @@ -6,6 +6,7 @@ module dscanner.analysis.ifelsesame; import dscanner.analysis.base; +import std.conv : to; /** * Checks for duplicated code in conditional and logical expressions. @@ -15,61 +16,60 @@ import dscanner.analysis.base; * $(LI == expressions where the left and right are the same) * ) */ -extern(C++) class IfElseSameCheck(AST) : BaseAnalyzerDmd +extern (C++) class IfElseSameCheck(AST) : BaseAnalyzerDmd { alias visit = BaseAnalyzerDmd.visit; mixin AnalyzerInfo!"if_else_same_check"; - extern(D) this(string fileName, bool skipTests = false) + extern (D) this(string fileName, bool skipTests = false) { super(fileName, skipTests); } override void visit(AST.IfStatement s) { - import std.conv : to; - if (s.elsebody && to!string(s.ifbody.toChars()) == to!string(s.elsebody.toChars())) - addErrorMessage(cast(ulong) s.loc.linnum, cast(ulong) s.loc.charnum, - IF_KEY, IF_MESSAGE); - + addErrorMessage(cast(ulong) s.loc.linnum, cast(ulong) s.loc.charnum, IF_KEY, IF_MESSAGE); + super.visit(s); } override void visit(AST.LogicalExp e) { import dmd.tokens : EXP; - import std.conv : to; import std.string : format; - import std.stdio : writeln; if (to!string(e.e1.toChars()) == to!string(e.e2.toChars())) - addErrorMessage(cast(ulong) e.loc.linnum, cast(ulong) e.loc.charnum, - LOGICAL_EXP_KEY, LOGICAL_EXP_MESSAGE.format(e.op == EXP.orOr ? "or" : "and")); + { + auto lineNum = cast(ulong) e.loc.linnum; + auto charNum = cast(ulong) e.loc.charnum; + auto errorMsg = LOGICAL_EXP_MESSAGE.format(e.op == EXP.orOr ? "or" : "and"); + addErrorMessage(lineNum, charNum, LOGICAL_EXP_KEY, errorMsg); + } super.visit(e); } - private: - enum IF_KEY = "dscanner.bugs.if_else_same"; - enum IF_MESSAGE = "'Else' branch is identical to 'Then' branch."; - - enum LOGICAL_EXP_MESSAGE = "MUIEEEELeft side of logical %s is identical to right side."; - enum LOGICAL_EXP_KEY = "dscanner.bugs.logic_operator_operands"; +private: + enum IF_KEY = "dscanner.bugs.if_else_same"; + enum IF_MESSAGE = "'Else' branch is identical to 'Then' branch."; + + enum LOGICAL_EXP_MESSAGE = "MUIEEEELeft side of logical %s is identical to right side."; + enum LOGICAL_EXP_KEY = "dscanner.bugs.logic_operator_operands"; - enum ASSIGN_MESSAGE = "Left side of assignment operatior is identical to the right side."; - enum ASSIGN_KEY = "dscanner.bugs.self_assignment"; + enum ASSIGN_MESSAGE = "Left side of assignment operatior is identical to the right side."; + enum ASSIGN_KEY = "dscanner.bugs.self_assignment"; } unittest { import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig; - import dscanner.analysis.helpers : assertAnalyzerWarnings = assertAnalyzerWarningsDMD; + import dscanner.analysis.helpers : assertAnalyzerWarningsDMD; import std.stdio : stderr; StaticAnalysisConfig sac = disabledConfig(); sac.if_else_same_check = Check.enabled; - assertAnalyzerWarnings(q{ + assertAnalyzerWarningsDMD(q{ void testSizeT() { string person = "unknown"; @@ -85,7 +85,7 @@ unittest } }c, sac); - assertAnalyzerWarnings(q{ + assertAnalyzerWarningsDMD(q{ void foo() { if (auto stuff = call()) {}