Add local import check
This commit is contained in:
parent
4c1df13262
commit
95c8b1b19a
|
@ -1 +1 @@
|
||||||
Subproject commit 27f45e6490e7ef0e9a6ef604e51f68a54b10ee2d
|
Subproject commit d57e38c169613edfa39088adb933fd220b6208f3
|
9
makefile
9
makefile
|
@ -3,14 +3,7 @@
|
||||||
DMD = dmd
|
DMD = dmd
|
||||||
GDC = gdc
|
GDC = gdc
|
||||||
LDC = ldc2
|
LDC = ldc2
|
||||||
SRC = src/main.d\
|
SRC = src/*.d\
|
||||||
src/stats.d\
|
|
||||||
src/imports.d\
|
|
||||||
src/highlighter.d\
|
|
||||||
src/ctags.d\
|
|
||||||
src/astprinter.d\
|
|
||||||
src/outliner.d\
|
|
||||||
src/symbol_finder.d\
|
|
||||||
src/analysis/*.d\
|
src/analysis/*.d\
|
||||||
libdparse/src/std/*.d\
|
libdparse/src/std/*.d\
|
||||||
libdparse/src/std/d/*.d\
|
libdparse/src/std/d/*.d\
|
||||||
|
|
|
@ -77,4 +77,7 @@ struct StaticAnalysisConfig
|
||||||
|
|
||||||
@INI("Checks for use of the comma operator")
|
@INI("Checks for use of the comma operator")
|
||||||
bool comma_expression_check;
|
bool comma_expression_check;
|
||||||
|
|
||||||
|
@INI("Checks for local imports that are too broad")
|
||||||
|
bool local_import_check;
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,73 @@
|
||||||
|
// Copyright Brian Schott (Hackerpilot) 2014.
|
||||||
|
// 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.local_imports;
|
||||||
|
|
||||||
|
import std.stdio;
|
||||||
|
import std.d.ast;
|
||||||
|
import std.d.lexer;
|
||||||
|
import analysis.base;
|
||||||
|
import analysis.helpers;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Checks for local imports that import all symbols.
|
||||||
|
* See_also: $(LINK https://issues.dlang.org/show_bug.cgi?id=10378)
|
||||||
|
*/
|
||||||
|
class LocalImportCheck : BaseAnalyzer
|
||||||
|
{
|
||||||
|
alias visit = BaseAnalyzer.visit;
|
||||||
|
|
||||||
|
this(string fileName)
|
||||||
|
{
|
||||||
|
super(fileName);
|
||||||
|
}
|
||||||
|
|
||||||
|
mixin visitThing!StructBody;
|
||||||
|
mixin visitThing!BlockStatement;
|
||||||
|
|
||||||
|
override void visit(const Declaration dec)
|
||||||
|
{
|
||||||
|
if (dec.importDeclaration is null)
|
||||||
|
{
|
||||||
|
dec.accept(this);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
foreach (attr; dec.attributes)
|
||||||
|
{
|
||||||
|
if (attr.storageClass !is null && attr.storageClass.token == tok!"static")
|
||||||
|
isStatic = true;
|
||||||
|
}
|
||||||
|
dec.accept(this);
|
||||||
|
isStatic = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
override void visit(const ImportDeclaration id)
|
||||||
|
{
|
||||||
|
if ((!isStatic && interesting) && (id.importBindings is null || id.importBindings.importBinds.length == 0))
|
||||||
|
{
|
||||||
|
addErrorMessage(id.singleImports[0].identifierChain.identifiers[0].line,
|
||||||
|
id.singleImports[0].identifierChain.identifiers[0].column,
|
||||||
|
"dscanner.suspicious.local_imports", "Local imports should specify"
|
||||||
|
~ " the symbols being imported to avoid hiding local symbols.");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private:
|
||||||
|
|
||||||
|
mixin template visitThing(T)
|
||||||
|
{
|
||||||
|
override void visit(const T thing)
|
||||||
|
{
|
||||||
|
auto b = interesting;
|
||||||
|
interesting = true;
|
||||||
|
thing.accept(this);
|
||||||
|
interesting = b;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
bool interesting;
|
||||||
|
bool isStatic;
|
||||||
|
}
|
||||||
|
|
|
@ -38,6 +38,7 @@ import analysis.stats_collector;
|
||||||
import analysis.undocumented;
|
import analysis.undocumented;
|
||||||
import analysis.comma_expression;
|
import analysis.comma_expression;
|
||||||
import analysis.function_attributes;
|
import analysis.function_attributes;
|
||||||
|
import analysis.local_imports;
|
||||||
|
|
||||||
bool first = true;
|
bool first = true;
|
||||||
|
|
||||||
|
@ -185,6 +186,7 @@ MessageSet analyze(string fileName, const Module m,
|
||||||
if (analysisConfig.undocumented_declaration_check) checks ~= new UndocumentedDeclarationCheck(fileName);
|
if (analysisConfig.undocumented_declaration_check) checks ~= new UndocumentedDeclarationCheck(fileName);
|
||||||
if (analysisConfig.function_attribute_check) checks ~= new FunctionAttributeCheck(fileName);
|
if (analysisConfig.function_attribute_check) checks ~= new FunctionAttributeCheck(fileName);
|
||||||
if (analysisConfig.comma_expression_check) checks ~= new CommaExpressionCheck(fileName);
|
if (analysisConfig.comma_expression_check) checks ~= new CommaExpressionCheck(fileName);
|
||||||
|
if (analysisConfig.local_import_check) checks ~= new LocalImportCheck(fileName);
|
||||||
|
|
||||||
foreach (check; checks)
|
foreach (check; checks)
|
||||||
{
|
{
|
||||||
|
|
Loading…
Reference in New Issue