-
Notifications
You must be signed in to change notification settings - Fork 83
Add check for asserts without an explanatory message #495
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
dlang-bot
merged 2 commits into
dlang-community:master
from
wilzbach:assert_without_msg
Dec 16, 2017
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| // 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.assert_without_msg; | ||
|
|
||
| import analysis.base : BaseAnalyzer; | ||
| import dsymbol.scope_ : Scope; | ||
| import dparse.lexer; | ||
| import dparse.ast; | ||
|
|
||
| import std.stdio; | ||
| import std.algorithm; | ||
|
|
||
| /** | ||
| * Check that all asserts have an explanatory message. | ||
| */ | ||
| class AssertWithoutMessageCheck : BaseAnalyzer | ||
| { | ||
| enum string KEY = "dscanner.style.assert_without_msg"; | ||
| enum string MESSAGE = "An assert should have an explanatory message"; | ||
|
|
||
| /// | ||
| this(string fileName, const(Scope)* sc, bool skipTests = false) | ||
| { | ||
| super(fileName, sc, skipTests); | ||
| } | ||
|
|
||
| override void visit(const AssertExpression expr) | ||
| { | ||
| if (expr.message is null) | ||
| addErrorMessage(expr.line, expr.column, KEY, MESSAGE); | ||
| } | ||
|
|
||
| override void visit(const FunctionCallExpression expr) | ||
| { | ||
| if (!isStdExceptionImported) | ||
| return; | ||
|
|
||
| if (expr.unaryExpression !is null && | ||
| expr.unaryExpression.primaryExpression !is null && | ||
| expr.unaryExpression.primaryExpression.identifierOrTemplateInstance !is null) | ||
| { | ||
| auto ident = expr.unaryExpression.primaryExpression.identifierOrTemplateInstance.identifier; | ||
| if (ident.text == "enforce" && expr.arguments !is null && expr.arguments.argumentList !is null && | ||
| expr.arguments.argumentList.items.length < 2) | ||
| addErrorMessage(ident.line, ident.column, KEY, MESSAGE); | ||
| } | ||
| } | ||
|
|
||
| override void visit(const SingleImport sImport) | ||
| { | ||
| static immutable stdException = ["std", "exception"]; | ||
| if (sImport.identifierChain.identifiers.map!(a => a.text).equal(stdException)) | ||
| isStdExceptionImported = true; | ||
| } | ||
|
|
||
| // revert the stack after new scopes | ||
| override void visit(const Declaration decl) | ||
| { | ||
| // be careful - ImportDeclarations don't introduce a new scope | ||
| if (decl.importDeclaration is null) | ||
| { | ||
| bool tmp = isStdExceptionImported; | ||
| scope(exit) isStdExceptionImported = tmp; | ||
| decl.accept(this); | ||
| } | ||
| else | ||
| decl.accept(this); | ||
| } | ||
|
|
||
| mixin ScopedVisit!IfStatement; | ||
| mixin ScopedVisit!BlockStatement; | ||
|
|
||
| alias visit = BaseAnalyzer.visit; | ||
|
|
||
| private: | ||
| bool isStdExceptionImported; | ||
|
|
||
| template ScopedVisit(NodeType) | ||
| { | ||
| override void visit(const NodeType n) | ||
| { | ||
| bool tmp = isStdExceptionImported; | ||
| scope(exit) isStdExceptionImported = tmp; | ||
| n.accept(this); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| unittest | ||
| { | ||
| import std.stdio : stderr; | ||
| import std.format : format; | ||
| import analysis.config : StaticAnalysisConfig, Check, disabledConfig; | ||
| import analysis.helpers : assertAnalyzerWarnings; | ||
|
|
||
| StaticAnalysisConfig sac = disabledConfig(); | ||
| sac.assert_without_msg = Check.enabled; | ||
|
|
||
| assertAnalyzerWarnings(q{ | ||
| unittest { | ||
| assert(0, "foo bar"); | ||
| assert(0); // [warn]: %s | ||
| } | ||
| }c.format( | ||
| AssertWithoutMessageCheck.MESSAGE, | ||
| ), sac); | ||
|
|
||
| assertAnalyzerWarnings(q{ | ||
| unittest { | ||
| static assert(0, "foo bar"); | ||
| static assert(0); // [warn]: %s | ||
| } | ||
| }c.format( | ||
| AssertWithoutMessageCheck.MESSAGE, | ||
| ), sac); | ||
|
|
||
| // check for std.exception.enforce | ||
| assertAnalyzerWarnings(q{ | ||
| unittest { | ||
| enforce(0); // std.exception not imported yet - could be a user-defined symbol | ||
| import std.exception; | ||
| enforce(0, "foo bar"); | ||
| enforce(0); // [warn]: %s | ||
| } | ||
| }c.format( | ||
| AssertWithoutMessageCheck.MESSAGE, | ||
| ), sac); | ||
|
|
||
| // check for std.exception.enforce | ||
| assertAnalyzerWarnings(q{ | ||
| unittest { | ||
| import exception; | ||
| class C { | ||
| import std.exception; | ||
| } | ||
| enforce(0); // std.exception not imported yet - could be a user-defined symbol | ||
| struct S { | ||
| import std.exception; | ||
| } | ||
| enforce(0); // std.exception not imported yet - could be a user-defined symbol | ||
| if (false) { | ||
| import std.exception; | ||
| } | ||
| enforce(0); // std.exception not imported yet - could be a user-defined symbol | ||
| { | ||
| import std.exception; | ||
| } | ||
| enforce(0); // std.exception not imported yet - could be a user-defined symbol | ||
| } | ||
| }c, sac); | ||
|
|
||
| stderr.writeln("Unittest for AssertWithoutMessageCheck passed."); | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bbasile FWIW I test for
assert(0)- just not in the way you would like it to be.