Skip to content
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

Replace libdparse with DMD in UselessInitializerChecker #121

Merged
merged 2 commits into from
Aug 12, 2024

Conversation

Vladiwostok
Copy link
Collaborator

@Vladiwostok Vladiwostok commented Apr 29, 2024

This check looks for useless initializers on:

  • numerical / boolean types (0);
  • nullable types (null);
  • basic types (.init);
  • empty arrays ([])
  • default struct init (.init).

This check also ignores variables declared with a DDoc comment (/// comment), typeof expressions and "compiles" trait expression. It also ignores disabled structs or structs having the default ctor disabled. This check is also limited when it comes to structs - it can only detect previously defined structs in the module. I've kept the same behavior when migrating to DMD.

@Vladiwostok Vladiwostok linked an issue Apr 29, 2024 that may be closed by this pull request
return;

assert(_inStruct.length > 1);
string structName = cast(string) structDecl.ident.toString();
Copy link
Collaborator

Choose a reason for hiding this comment

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

isn't the cast useless here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think so. Identifier#toString() returns const(char)[]. Isn't the D string an alias for immutable(char)[]?

Copy link
Collaborator

@RazvanN7 RazvanN7 Aug 12, 2024

Choose a reason for hiding this comment

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

Hmm, I expected that toString returns a D string.

assert(_inStruct.length > 1);
string structName = cast(string) structDecl.ident.toString();
if (isNestedStruct())
structName = structStack[$ - 1] ~ "." ~ structName;
Copy link
Collaborator

Choose a reason for hiding this comment

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

Why are you constructing the qualified name here? if you call "toPrettyChars" on a StructDeclaration it will give you the fully qualified name which is a good unique identifier.

Copy link
Collaborator

Choose a reason for hiding this comment

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

Hmm... on a second thought, it might be possible that toPrettyChars doesn't work correctly here if you are not performing semantic. Please check whether it works or not, because if it does it will save you some pain.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Only nested structs are counted. Wouldn't toPrettyChars also print the encapsulating class, if present?

src/dscanner/analysis/useless_initializer.d Outdated Show resolved Hide resolved
visitedStructs[structName].shouldErrorOnInit = !isDisabled;
}

override void visit(AST.VarDeclaration varDeclaration)
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please try to find smaller names that are congruent with the dmd codebase. For example, vd is consistently used for variable declarations variables throughout dmd. If that seems not expressive enough, you can use varDecl. Having smaller if conditions in length is eye candy.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I can shorten it, however for a function this long a longer name is better - I prefer shorter names in local / small scopes. vd is an awful name though

@RazvanN7 RazvanN7 merged commit 655fc46 into Dlang-UPB:replace_libdparse Aug 12, 2024
17 checks passed
@Vladiwostok Vladiwostok deleted the useless-init branch August 12, 2024 11:48
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.

Replace libdparse with DMD in UselessInitializerChecker
2 participants