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