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.enumarrayliteral; 7 8 import dparse.ast; 9 import dparse.lexer; 10 import dscanner.analysis.base; 11 import std.algorithm : canFind, map; 12 import dsymbol.scope_ : Scope; 13 14 void doNothing(string, size_t, size_t, string, bool) 15 { 16 } 17 18 final class EnumArrayLiteralCheck : BaseAnalyzer 19 { 20 alias visit = BaseAnalyzer.visit; 21 22 this(string fileName, const(Scope)* sc, bool skipTests = false) 23 { 24 super(fileName, sc, skipTests); 25 } 26 27 bool looking; 28 29 mixin visitTemplate!ClassDeclaration; 30 mixin visitTemplate!InterfaceDeclaration; 31 mixin visitTemplate!UnionDeclaration; 32 mixin visitTemplate!StructDeclaration; 33 34 override void visit(const AutoDeclaration autoDec) 35 { 36 if (autoDec.storageClasses.canFind!(a => a.token == tok!"enum")) 37 { 38 foreach (part; autoDec.parts) 39 { 40 if (part.initializer is null) 41 continue; 42 if (part.initializer.nonVoidInitializer is null) 43 continue; 44 if (part.initializer.nonVoidInitializer.arrayInitializer is null) 45 continue; 46 addErrorMessage(part.identifier.line, part.identifier.column, 47 "dscanner.performance.enum_array_literal", 48 "This enum may lead to unnecessary allocation at run-time." 49 ~ " Use 'static immutable " 50 ~ part.identifier.text ~ " = [ ...' instead."); 51 } 52 } 53 autoDec.accept(this); 54 } 55 }