Skip to content

Commit

Permalink
replace libdparse in trust_too_much visitor (#70)
Browse files Browse the repository at this point in the history
  • Loading branch information
lucica28 committed May 29, 2023
1 parent 9a9d285 commit 8360dab
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 62 deletions.
10 changes: 6 additions & 4 deletions src/dscanner/analysis/run.d
Original file line number Diff line number Diff line change
Expand Up @@ -549,10 +549,6 @@ MessageSet analyze(string fileName, const Module m, const StaticAnalysisConfig a
checks ~= new IfConstraintsIndentCheck(fileName, tokens,
analysisConfig.if_constraints_indent == Check.skipTests && !ut);

if (moduleName.shouldRun!TrustTooMuchCheck(analysisConfig))
checks ~= new TrustTooMuchCheck(fileName,
analysisConfig.trust_too_much == Check.skipTests && !ut);

if (moduleName.shouldRun!RedundantStorageClassCheck(analysisConfig))
checks ~= new RedundantStorageClassCheck(fileName,
analysisConfig.redundant_storage_classes == Check.skipTests && !ut);
Expand Down Expand Up @@ -636,6 +632,12 @@ MessageSet analyzeDmd(string fileName, ASTCodegen.Module m, const char[] moduleN
fileName,
config.opequals_tohash_check == Check.skipTests && !ut
);

if (moduleName.shouldRunDmd!(TrustTooMuchCheck!ASTCodegen)(config))
visitors ~= new TrustTooMuchCheck!ASTCodegen(
fileName,
config.trust_too_much == Check.skipTests && !ut
);

if (moduleName.shouldRunDmd!(AutoRefAssignmentCheck!ASTCodegen)(config))
visitors ~= new AutoRefAssignmentCheck!ASTCodegen(fileName);
Expand Down
78 changes: 20 additions & 58 deletions src/dscanner/analysis/trust_too_much.d
Original file line number Diff line number Diff line change
Expand Up @@ -5,88 +5,50 @@

module dscanner.analysis.trust_too_much;

import std.stdio;
import dparse.ast;
import dparse.lexer;
import dscanner.analysis.base;
import dsymbol.scope_;
import dmd.astenums : STC;

/**
* Checks that `@trusted` is only applied to a a single function
*/
final class TrustTooMuchCheck : BaseAnalyzer
extern(C++) class TrustTooMuchCheck(AST) : BaseAnalyzerDmd
{
private:
mixin AnalyzerInfo!"trust_too_much";
alias visit = BaseAnalyzerDmd.visit;

static immutable MESSAGE = "Trusting a whole scope is a bad idea, " ~
private:
extern(D) static immutable MESSAGE = "Trusting a whole scope is a bad idea, " ~
"`@trusted` should only be attached to the functions individually";
static immutable string KEY = "dscanner.trust_too_much";

bool checkAtAttribute = true;
extern(D) static immutable string KEY = "dscanner.trust_too_much";

public:

alias visit = BaseAnalyzer.visit;

mixin AnalyzerInfo!"trust_too_much";

///
this(string fileName, bool skipTests = false)
extern(D) this(string fileName, bool skipTests = false)
{
super(fileName, sc, skipTests);
super(fileName, skipTests);
}

override void visit(const AtAttribute d)
override void visit(AST.StorageClassDeclaration scd)
{
if (checkAtAttribute && d.identifier.text == "trusted")
{
const Token t = d.identifier;
addErrorMessage(t.line, t.column, KEY, MESSAGE);
}
d.accept(this);
}

// always applied to function body, so OK
override void visit(const MemberFunctionAttribute d)
{
const oldCheckAtAttribute = checkAtAttribute;
checkAtAttribute = false;
d.accept(this);
checkAtAttribute = oldCheckAtAttribute;
}

// handles `@trusted{}` and old style, leading, atAttribute for single funcs
override void visit(const Declaration d)
{
const oldCheckAtAttribute = checkAtAttribute;

checkAtAttribute = d.functionDeclaration is null && d.unittest_ is null &&
d.constructor is null && d.destructor is null &&
d.staticConstructor is null && d.staticDestructor is null &&
d.sharedStaticConstructor is null && d.sharedStaticDestructor is null;
d.accept(this);
checkAtAttribute = oldCheckAtAttribute;
}

// issue #588
override void visit(const AliasDeclaration d)
{
const oldCheckAtAttribute = checkAtAttribute;
checkAtAttribute = false;
d.accept(this);
checkAtAttribute = oldCheckAtAttribute;
if (scd.stc & STC.trusted)
addErrorMessage(cast(ulong) scd.loc.linnum, cast(ulong) scd.loc.charnum,
KEY, MESSAGE);

super.visit(scd);
}
}

unittest
{
import dscanner.analysis.config : StaticAnalysisConfig, Check, disabledConfig;
import dscanner.analysis.helpers : assertAnalyzerWarnings;
import dscanner.analysis.helpers : assertAnalyzerWarnings = assertAnalyzerWarningsDMD;
import std.format : format;
import std.stdio : stderr;

StaticAnalysisConfig sac = disabledConfig();
sac.trust_too_much = Check.enabled;
const msg = TrustTooMuchCheck.MESSAGE;
const msg = "Trusting a whole scope is a bad idea, " ~
"`@trusted` should only be attached to the functions individually";

//--- fail cases ---//

Expand Down Expand Up @@ -157,4 +119,4 @@ unittest
}c , sac);

stderr.writeln("Unittest for TrustTooMuchCheck passed.");
}
}

0 comments on commit 8360dab

Please sign in to comment.