Skip to content

Add ignoreDeadCode option#633

Draft
aosen-xiong wants to merge 22 commits intoeisop:masterfrom
aosen-xiong:add-not-check-dead-code-option
Draft

Add ignoreDeadCode option#633
aosen-xiong wants to merge 22 commits intoeisop:masterfrom
aosen-xiong:add-not-check-dead-code-option

Conversation

@aosen-xiong
Copy link
Copy Markdown
Collaborator

Fixes #627

Do you need more test cases on this option?

Copy link
Copy Markdown
Member

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

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

Thanks for starting work on this issue!
At the moment this isn't tested at all, so please do add a new test directory that enables the option.
When I tried it before, there was a problem with the Nullness Checker, so do use that.
Write tests e.g. like the one in the issue and things like an empty try block where an impossible catch block does something bad.

"warns",

// Make checker ignore the expression in dead branch
"notCheckDeadCode",
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

How about ignoreDeadCode to have more symmetry with other ignoreXXX options?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

By convention, for each option we list the main point where that option is used. Please add such a reference.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

By convention, for each option we list the main point where that option is used. Please add such a reference.

Thanks, already addressed the comment in 1d1c348

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

How about ignoreDeadCode to have more symmetry with other ignoreXXX options?

addressed this comment in 58f4773

}
});
*/
if (checker.hasOption("notCheckDeadCode")) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Here, and probably in the BaseTypeVisitor, put the option into a protected variable, so that we don't look up the option multiple times. See how other options are handled.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Here, and probably in the BaseTypeVisitor, put the option into a protected variable, so that we don't look up the option multiple times. See how other options are handled.

addressed this comment in 2540cd3

@aosen-xiong
Copy link
Copy Markdown
Collaborator Author

Thanks for starting work on this issue! At the moment this isn't tested at all, so please do add a new test directory that enables the option. When I tried it before, there was a problem with the Nullness Checker, so do use that. Write tests e.g. like the one in the issue and things like an empty try block where an impossible catch block does something bad.

Thanks, I will add test cases later.

Copy link
Copy Markdown
Member

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

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

@Ao-senXiong Were there more tests you wanted to add? Can you wrap up this PR?

@aosen-xiong aosen-xiong requested a review from wmdietl December 17, 2024 15:30
@aosen-xiong aosen-xiong assigned wmdietl and unassigned aosen-xiong Dec 17, 2024
@wmdietl wmdietl changed the title Adding notcheckdeadcode option Add ignoreDeadCode option Dec 31, 2024
Copy link
Copy Markdown
Member

@wmdietl wmdietl left a comment

Choose a reason for hiding this comment

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

Thanks for the updates!

// :: error: (dereference.of.nullable)
obj.toString();
} else {
// TODO: This is a dead branch should not issue error, the currently it does
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As this is the point of the PR, we should fix this issue before merging.

@wmdietl wmdietl assigned aosen-xiong and unassigned wmdietl Dec 31, 2024
obj.toString();
// The following loop is dead code because the loop condition is false.
for (int i = 0; i < 0; i++) {
obj.toString();
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Also note that this also doesn't produce a warning before this PR - so does this PR do anything?
The deref on obj before is remembered and then we don't produce multiple errors. So, here and below, don't use the same variable outside the loop and inside.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

You should add the same test to the normal Nullness Checker tests and show the actual difference in behavior between the two.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I see. I thought we want to enable the check first. I will write dead branch analysis in this PR. I think Jenny's thesis contains case study for dead branch analysis but not sure why it is not in the dataflow framework.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

What do you mean? The GenericAnnotatedTypeFactory#reachableNodes should already contain whether a node is reachable or not, right? So what additional analysis do you want to add? In #627 it sounds like all the infrastructure is already there, we just wanted to guard it by an option.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

I think this issue comes from when we fetch upstream changes. They only implement reachablenodes to determine whether it includes exception node and its Successors. See typetool#6246

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

That upstream change is already in eisop. Are you saying there are other changes that have not been merged yet?

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Sorry, I am trying to say the upstream change is not enough to check dead branch for if else and while loop.

Co-authored-by: Werner Dietl <wdietl@gmail.com>
@aosen-xiong
Copy link
Copy Markdown
Collaborator Author

This PR opprop#153 seems quite relavent.

@aosen-xiong aosen-xiong requested a review from wmdietl January 5, 2025 04:28
@aosen-xiong aosen-xiong assigned wmdietl and unassigned aosen-xiong Jan 5, 2025
@wmdietl
Copy link
Copy Markdown
Member

wmdietl commented Jan 5, 2025

So the only difference would be for this test.
It would make sense to have a copy of this test with this PR (and cross-reference the two files).

I'm not sure this option is particularly valuable at the moment... that TryCatch example does not seem worth adding an option for.

How about we convert this PR to a draft until it is more useful, by adding an actual dead code analysis?

@wmdietl wmdietl assigned aosen-xiong and unassigned wmdietl Jan 5, 2025
@aosen-xiong aosen-xiong marked this pull request as draft January 5, 2025 22:01
@aosen-xiong aosen-xiong removed their assignment Jan 5, 2025
@wmdietl
Copy link
Copy Markdown
Member

wmdietl commented Mar 23, 2026

A comparison to the approach in #101 would be good. I guess we only need one of the two PRs.

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.

Add an option to not check dead code

2 participants