1 // Copyright Brian Schott (Hackerpilot) 2016. 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.alias_syntax_check; 7 8 import dparse.ast; 9 import dparse.lexer; 10 import dscanner.analysis.base; 11 12 /** 13 * Checks for uses of the old alias syntax. 14 */ 15 class AliasSyntaxCheck : BaseAnalyzer 16 { 17 alias visit = BaseAnalyzer.visit; 18 19 this(string fileName, bool skipTests = false) 20 { 21 super(fileName, null, skipTests); 22 } 23 24 override void visit(const AliasDeclaration ad) 25 { 26 if (ad.declaratorIdentifierList is null) 27 return; 28 assert(ad.declaratorIdentifierList.identifiers.length > 0, 29 "Identifier list length is zero, libdparse has a bug"); 30 addErrorMessage(ad.declaratorIdentifierList.identifiers[0].line, 31 ad.declaratorIdentifierList.identifiers[0].column, KEY, 32 "Prefer the new \"'alias' identifier '=' type ';'\" syntax" 33 ~ " to the old \"'alias' type identifier ';'\" syntax."); 34 } 35 36 private: 37 enum KEY = "dscanner.style.alias_syntax"; 38 } 39 40 unittest 41 { 42 import dscanner.analysis.helpers : assertAnalyzerWarnings; 43 import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig; 44 import std.stdio : stderr; 45 46 StaticAnalysisConfig sac = disabledConfig(); 47 sac.alias_syntax_check = Check.enabled; 48 assertAnalyzerWarnings(q{ 49 alias int abcde; // [warn]: Prefer the new "'alias' identifier '=' type ';'" syntax to the old "'alias' type identifier ';'" syntax. 50 alias abcde = int; 51 }c, sac); 52 53 stderr.writeln("Unittest for AliasSyntaxCheck passed."); 54 }