Fixes cases of false or non positive with the useless init check#475
Conversation
768325b to
106806e
Compare
|
Right now with these fixes i just have 1 warning in phobos.
But of course also after a couple of changes in phobos: https://github.com/BBasile/phobos/commit/0994227197b7a27dfc879466973470758072b35c |
|
This is good now. After this phobos PR and if you run dscanner -S | grep "initializer is useless" > dscanner.txtin phobos repository then a 0 Kio file is produced. |
wilzbach
left a comment
There was a problem hiding this comment.
Is there any reason why you seem to prefer filter!(lambda).empty over canFind!lambda?
In general this LGTM.
src/analysis/useless_initializer.d
Outdated
| DynamicArray!(string) _structStack; | ||
| DynamicArray!(bool) _inStruct; | ||
| DynamicArray!(bool) _atDisabled; | ||
| DynamicArray!(bool) _inTest; |
There was a problem hiding this comment.
nit: Hmm I am not a huge fan of using _ if there's a proper type system with private in place.
src/analysis/useless_initializer.d
Outdated
|
|
||
| override void visit(const(StructDeclaration) decl) | ||
| { | ||
| if (_inTest.back()) |
There was a problem hiding this comment.
If you always just test the end of the array, wouldn't a simple bool flag suffice?
There was a problem hiding this comment.
because of this case:
unittest{struct A{unittest{}}}
src/analysis/useless_initializer.d
Outdated
| // initializer has to appear clearly in generated ddoc | ||
| decl.comment !is null || | ||
| // issue 474, manifest constants HAVE to be initialized. | ||
| !decl.storageClasses.filter!(a => a.token == tok!"enum").empty) |
src/analysis/useless_initializer.d
Outdated
| import dparse.ast; | ||
| import dparse.lexer; | ||
| import std.algorithm : among, canFind; | ||
| import std.algorithm.iteration : filter; |
There was a problem hiding this comment.
Why not simply importing entire std.algorithm - it's very commonly used... and saves the pain of updating this list all the time.
src/analysis/useless_initializer.d
Outdated
| enum warn = q{addErrorMessage(declarator.name.line, declarator.name.column, | ||
| key, msg);}; | ||
| } | ||
| else |
There was a problem hiding this comment.
Off-topic: I am not a huge friend of this pattern. While I do the see merits of being able to just use X, the string formatting could (in theory of course) be broken.
src/analysis/useless_initializer.d
Outdated
| if (_inStruct.length > 1 && _inStruct[$-2] && | ||
| ((decl.parameters && decl.parameters.parameters.length == 0) || !decl.parameters)) | ||
| { | ||
| _structCanBeInit[_structStack.back()] = !_atDisabled[$-1]; |
There was a problem hiding this comment.
While it shouldn't matter, we could save the duplicate hashmap query here:
bool canBeInit = !_atDisabled[$-1];
if ...
_structCanBeInit[_structStack.back()] = canBeInit
src/analysis/useless_initializer.d
Outdated
| static immutable intDefs = ["0", "0L", "0UL", "0uL", "0U", "0x0", "0b0"]; | ||
|
|
||
| HashMap!(string, bool) _structCanBeInit; | ||
| DynamicArray!(string) _structStack; |
There was a problem hiding this comment.
As mentioned below: I think _structStack and _atDisabled don't need to be arrays.
src/analysis/useless_initializer.d
Outdated
| if (_inTest.back()) | ||
| return; | ||
|
|
||
| _structStack.insert(decl.name.text); |
There was a problem hiding this comment.
Hmm are you sure this would be unique? Wouldn't you need proper mangling?
Consider:
struct F {
struct G {}
G g;
}
struct G {
struct G {} // collision
G g;
}- FQN the struct names - prevent a double query in the canBeInit AA - import the whole also package - there was not test on non-initilized variables
| protected: | ||
|
|
||
| bool inAggregate = false; | ||
| bool inAggregate; |
There was a problem hiding this comment.
Looks like there's a tab here instead of spaces, even before the change. Might be a good idea to setup some code guidelines and checks for them, not sure if there were any before.
There was a problem hiding this comment.
For sure there's one. @Hackerpilot uses tabs on his stuff. Excepted libdparse because it was planned to be included in phobos.
There was a problem hiding this comment.
The .editorconfig file for this project does specify tabs.
wilzbach
left a comment
There was a problem hiding this comment.
LGTM.
Small nits (I don't care about them)
_atDisabledand_structStackcould be replaced by a flag?- weird whitespace in
duplicate_attribute.d
src/analysis/duplicate_attribute.d
Outdated
| bool hasNoThrow = false; | ||
| bool hasProperty; | ||
| bool hasSafe ; | ||
| bool hasTrusted ; |
There was a problem hiding this comment.
I guess the whitespace before ; is accidental?
src/analysis/useless_initializer.d
Outdated
| { | ||
| _atDisabled[$-1] = decl.attributes | ||
| .canFind!(a => a.atAttribute !is null && a.atAttribute.identifier.text == "disable"); | ||
| } |
There was a problem hiding this comment.
OT: At some point we probably should start sharing code between the checkers. I imagine sth. like isInDisabledStruct to be a fairly common request.
src/analysis/useless_initializer.d
Outdated
| continue; | ||
| if (!declarator.initializer || !declarator.initializer.nonVoidInitializer) | ||
| continue; | ||
| return; |
There was a problem hiding this comment.
Speaking of tabs, this looks like whitespace here. Should we maybe enable a checker on Travis, s.t. we can't push files which are beginning with tabs?
There was a problem hiding this comment.
(Sorry that I didn't realize this earlier - I don't care about the holy war of tabs vs. whitespace, but we should try to keep this repo consistent)
There was a problem hiding this comment.
I'm gonna patch this. I still have to add editorconfig support in Coedit. Currently the tab style is autodetected and for a new module the user preference is used, so spaces here.
|
Thanks for your pull request, @bbasile! |
Ok. The history got moderately long for this diff. Do you want to squash everything before merging? |
|
Yes squash. If GH wouldn't allow to do it i would have rebased and marked |
…ng-community#475) * fix dlang-community#474 fix dlang-community#473 fix dlang-community#476 - Cases of false and non positive with the useless init check * do not warn on documented variables * fix dlang-community#477 - Custom type initialized to init should not trigger a warn * allow struct.init when know struct has `@disable` ctor * fix last false detection in phobos * prevent check in the "compiles" trait * - use canFind when filter.empty was negated - FQN the struct names - prevent a double query in the canBeInit AA - import the whole also package - there was not test on non-initilized variables * fix, self-linting missed a case that was not yet fixed * fix more undetected warns during self linting * use a flag instead of a stack + apply skipTests * convert spaces to tabs
This PR fixes 4 bugs found while applying the useless initializer check to phobos.