From 76765a621de197e1ba7eaa1eb45d374d218390f1 Mon Sep 17 00:00:00 2001 From: Hackerpilot Date: Wed, 2 Mar 2016 21:11:38 -0800 Subject: [PATCH] Move old alias syntax warning out of the parser, see Hackerpilot/libdparse#80 --- libdparse | 2 +- src/analysis/alias_syntax_check.d | 54 +++++++++++++++++++++++++++++++ src/analysis/config.d | 3 ++ src/analysis/run.d | 3 ++ 4 files changed, 61 insertions(+), 1 deletion(-) create mode 100644 src/analysis/alias_syntax_check.d diff --git a/libdparse b/libdparse index adb134c..a10f598 160000 --- a/libdparse +++ b/libdparse @@ -1 +1 @@ -Subproject commit adb134c054ffcb05c74d7033e59617dedba1493b +Subproject commit a10f5988b2b130ad2cae28c90a668067f98c82a7 diff --git a/src/analysis/alias_syntax_check.d b/src/analysis/alias_syntax_check.d new file mode 100644 index 0000000..939f19c --- /dev/null +++ b/src/analysis/alias_syntax_check.d @@ -0,0 +1,54 @@ +// Copyright Brian Schott (Hackerpilot) 2016. +// Distributed under the Boost Software License, Version 1.0. +// (See accompanying file LICENSE_1_0.txt or copy at +// http://www.boost.org/LICENSE_1_0.txt) + +module analysis.alias_syntax_check; + +import dparse.ast; +import dparse.lexer; +import analysis.base; + +/** + * Checks for uses of the old alias syntax. + */ +class AliasSyntaxCheck : BaseAnalyzer +{ + alias visit = BaseAnalyzer.visit; + + this(string fileName) + { + super(fileName, null); + } + + override void visit(const AliasDeclaration ad) + { + if (ad.identifierList is null) + return; + assert(ad.identifierList.identifiers.length > 0, + "Identifier list length is zero, libdparse has a bug"); + addErrorMessage(ad.identifierList.identifiers[0].line, + ad.identifierList.identifiers[0].column, KEY, + "Prefer the new \"'alias' identifier '=' type ';'\" syntax" + ~ " to the old \"'alias' type identifier ';'\" syntax."); + } + +private: + enum KEY = "dscanner.style.alias_syntax"; +} + +unittest +{ + import analysis.helpers : assertAnalyzerWarnings; + import analysis.config : StaticAnalysisConfig; + import std.stdio : stderr; + + StaticAnalysisConfig sac; + sac.alias_syntax_check = true; + assertAnalyzerWarnings(q{ + alias int abcde; // [warn]: Prefer the new "'alias' identifier '=' type ';'" syntax to the old "'alias' type identifier ';'" syntax. + alias abcde = int; + }c, sac); + + stderr.writeln("Unittest for AliasSyntaxCheck passed."); +} diff --git a/src/analysis/config.d b/src/analysis/config.d index 88b37b4..961b5cd 100644 --- a/src/analysis/config.d +++ b/src/analysis/config.d @@ -110,4 +110,7 @@ struct StaticAnalysisConfig @INI("Checks for asserts that are always true") bool useless_assert_check; + + @INI("Check for uses of the old-style alias syntax") + bool alias_syntax_check; } diff --git a/src/analysis/run.d b/src/analysis/run.d index a8a41a1..b2e2de0 100644 --- a/src/analysis/run.d +++ b/src/analysis/run.d @@ -55,6 +55,7 @@ import analysis.line_length; import analysis.auto_ref_assignment; import analysis.incorrect_infinite_range; import analysis.useless_assert; +import analysis.alias_syntax_check; import dsymbol.string_interning : internString; import dsymbol.scope_; @@ -266,6 +267,8 @@ MessageSet analyze(string fileName, const Module m, const StaticAnalysisConfig a checks ~= new IncorrectInfiniteRangeCheck(fileName); if (analysisConfig.useless_assert_check) checks ~= new UselessAssertCheck(fileName); + if (analysisConfig.alias_syntax_check) + checks ~= new AliasSyntaxCheck(fileName); version (none) if (analysisConfig.redundant_if_check) checks ~= new IfStatementCheck(fileName, moduleScope);