Skip to content

Fixes cases of false or non positive with the useless init check#475

Merged
11 commits merged intomasterfrom
issue-474
Jun 28, 2017
Merged

Fixes cases of false or non positive with the useless init check#475
11 commits merged intomasterfrom
issue-474

Conversation

@ghost
Copy link

@ghost ghost commented Jun 22, 2017

This PR fixes 4 bugs found while applying the useless initializer check to phobos.

@ghost ghost changed the title fix #474 - Case of false positive with the useless init check : manifest constants fix #474 fix #475 - Cases of false positive with the useless init check Jun 22, 2017
@ghost ghost changed the title fix #474 fix #475 - Cases of false positive with the useless init check fix #474 fix #473 - Cases of false positive with the useless init check Jun 22, 2017
@ghost ghost force-pushed the issue-474 branch 3 times, most recently from 768325b to 106806e Compare June 22, 2017 19:10
@ghost ghost changed the title fix #474 fix #473 - Cases of false positive with the useless init check fix #474 fix #473 fix #475 - Cases of false and non positive with the useless init check Jun 22, 2017
@ghost ghost force-pushed the issue-474 branch from 1bf6068 to 2c3eff7 Compare June 22, 2017 19:41
@ghost ghost changed the title fix #474 fix #473 fix #475 - Cases of false and non positive with the useless init check [WIP ] Fixes cases of false or non positive with the useless init check Jun 22, 2017
@ghost
Copy link
Author

ghost commented Jun 23, 2017

Right now with these fixes i just have 1 warning in phobos.

./std/conv.d(4385:28)[warn]: Variable init initializer is useless because it does not differ from the default value

But of course also after a couple of changes in phobos: https://github.com/BBasile/phobos/commit/0994227197b7a27dfc879466973470758072b35c

@ghost ghost changed the title [WIP ] Fixes cases of false or non positive with the useless init check Fixes cases of false or non positive with the useless init check Jun 23, 2017
@ghost
Copy link
Author

ghost commented Jun 23, 2017

This is good now. After this phobos PR and if you run

dscanner -S | grep "initializer is useless" > dscanner.txt

in phobos repository then a 0 Kio file is produced.

@ghost ghost mentioned this pull request Jun 25, 2017
Copy link
Member

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason why you seem to prefer filter!(lambda).empty over canFind!lambda?

In general this LGTM.

DynamicArray!(string) _structStack;
DynamicArray!(bool) _inStruct;
DynamicArray!(bool) _atDisabled;
DynamicArray!(bool) _inTest;
Copy link
Member

@wilzbach wilzbach Jun 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Hmm I am not a huge fan of using _ if there's a proper type system with private in place.


override void visit(const(StructDeclaration) decl)
{
if (_inTest.back())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you always just test the end of the array, wouldn't a simple bool flag suffice?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because of this case:

unittest{struct A{unittest{}}}

// 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)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about using canFind?

import dparse.ast;
import dparse.lexer;
import std.algorithm : among, canFind;
import std.algorithm.iteration : filter;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not simply importing entire std.algorithm - it's very commonly used... and saves the pain of updating this list all the time.

enum warn = q{addErrorMessage(declarator.name.line, declarator.name.column,
key, msg);};
}
else
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

if (_inStruct.length > 1 && _inStruct[$-2] &&
((decl.parameters && decl.parameters.parameters.length == 0) || !decl.parameters))
{
_structCanBeInit[_structStack.back()] = !_atDisabled[$-1];
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While it shouldn't matter, we could save the duplicate hashmap query here:

bool canBeInit = !_atDisabled[$-1];
if ...

_structCanBeInit[_structStack.back()] = canBeInit

static immutable intDefs = ["0", "0L", "0UL", "0uL", "0U", "0x0", "0b0"];

HashMap!(string, bool) _structCanBeInit;
DynamicArray!(string) _structStack;
Copy link
Member

@wilzbach wilzbach Jun 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As mentioned below: I think _structStack and _atDisabled don't need to be arrays.

if (_inTest.back())
return;

_structStack.insert(decl.name.text);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll use the FQN

@ghost ghost force-pushed the issue-474 branch from 8930146 to 313e343 Compare June 25, 2017 22:12
@ghost ghost force-pushed the issue-474 branch from b8bf97c to 7ef6c4b Compare June 25, 2017 22:39
@ghost ghost deleted a comment from dlang-bot Jun 25, 2017
protected:

bool inAggregate = false;
bool inAggregate;
Copy link
Contributor

@skl131313 skl131313 Jun 25, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For sure there's one. @Hackerpilot uses tabs on his stuff. Excepted libdparse because it was planned to be included in phobos.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .editorconfig file for this project does specify tabs.

@ghost ghost mentioned this pull request Jun 28, 2017
Copy link
Member

@wilzbach wilzbach left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

Small nits (I don't care about them)

  • _atDisabled and _structStack could be replaced by a flag?
  • weird whitespace in duplicate_attribute.d

bool hasNoThrow = false;
bool hasProperty;
bool hasSafe ;
bool hasTrusted ;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess the whitespace before ; is accidental?

{
_atDisabled[$-1] = decl.attributes
.canFind!(a => a.atAttribute !is null && a.atAttribute.identifier.text == "disable");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OT: At some point we probably should start sharing code between the checkers. I imagine sth. like isInDisabledStruct to be a fairly common request.

@ghost ghost deleted a comment from dlang-bot Jun 28, 2017
continue;
if (!declarator.initializer || !declarator.initializer.nonVoidInitializer)
continue;
return;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(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)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@dlang-bot
Copy link
Contributor

Thanks for your pull request, @bbasile!

@wilzbach
Copy link
Member

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.

Ok. The history got moderately long for this diff. Do you want to squash everything before merging?
(I don't mind either way).
Btw should we define a guideline for this at https://github.com/dlang-community/discussions?

@ghost
Copy link
Author

ghost commented Jun 28, 2017

Yes squash. If GH wouldn't allow to do it i would have rebased and marked fixup all the commits.

@ghost ghost merged commit f89d356 into master Jun 28, 2017
@ghost ghost deleted the issue-474 branch July 5, 2017 07:36
wilzbach pushed a commit to wilzbach/Dscanner that referenced this pull request Jul 8, 2017
…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 pull request was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants