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

Expression tree inspection for redundant signs, parentheses and operators #2222

Open
ThunderFrame opened this issue Sep 6, 2016 · 7 comments
Labels
enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections
Milestone

Comments

@ThunderFrame
Copy link
Member

VBA allows for multiple negative signs. In most cases, they can be simplified.

  Dim bankBalance As Double

  bankBalance = -----1 ' -1
  bankBalance = ----1  '  1
  bankBalance = ---1#  ' -1#
  bankBalance = ----1& '  1&
@ThunderFrame
Copy link
Member Author

lol - #2222 for an issue about repeated characters 😄

@retailcoder retailcoder changed the title Inspection for redundant negation signs Expression tree inspection for redundant signs, parentheses and operators Sep 6, 2016
@retailcoder
Copy link
Member

I think this inspection needs to be a bit broader - it's going to have to analyze expression trees, and doing that only to issue an inspection result the odd time a double negative sign is encountered seems a bit of a waste. Let's make it about redundant signs, parentheses and operators - and make it trip when it encounters convoluted expressions such as:

If Not (foo <> 42) Then

..which would simplify/fix to:

If foo = 42 Then

@retailcoder
Copy link
Member

Inspection for redundant parentheses will be a problem until #2206 is fixed.

@retailcoder retailcoder added this to the Version 3.0 milestone Sep 11, 2016
@Vogel612 Vogel612 added enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. and removed feature-request labels Aug 11, 2017
@ThunderFrame
Copy link
Member Author

Could we perhaps make a distinction between expressions that can be optimized, and expressions that are just wrapped in parentheses for no good reason?

Optimization seems potentially hard:
If Not (foo <> 42) Then
If foo = 42 Then

These seem easier:
If (foo = 42) Then
If foo = 42 Then

Do: i = i + 1: Loop While (a(i) < v)
Do: i = i + 1: Loop While a(i) < v

@PeterMTaylor
Copy link

Would RegEx compliment or complicate this?

@ThunderFrame
Copy link
Member Author

Regex would probably be inadequate. You need to find balanced parentheses around an expression, where removing the parentheses would result in an identical expression.

This expression starts and ends with ( and ), but the removal of the parentheses would result in an invalid expression:

x = (1 - 2) / (3 + 4) becomes a syntax error: x = 1 - 2) / (3 + 4

This expression is still valid, after the removal of the surrounding parentheses (even if there is further optimization possible:

x = (1 + (2 + 3)) becomes x = 1 + (2 + 3)

@ThunderFrame
Copy link
Member Author

we should be able to identify the most basic forms:

x = (True)
If (True) Then
While (True)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Feature requests, or enhancements to existing features. Ideas. Anything within the project's scope. feature-inspections
Projects
None yet
Development

No branches or pull requests

4 participants