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

Fix parenthesis simplification around binary expressions #17241

Merged
merged 4 commits into from Feb 22, 2017

Conversation

DustinCampbell
Copy link
Member

Fixes #11958

This change adds a couple of semantic checks when determining whether parentheses can be removed from around a addition or multiplication expression contained within another addition or multiplication expression. For example, a + (b + c) or a * (b * c). Essentially, we're now a bit more conservative and will only remove parentheses if the following semantic rules hold true:

  1. The inner binary expression must not result in a user-defined operator being called.
  2. The type and converted type of binary expression must match.

This ensures that we don't incorrectly remove parentheses in several cases, such as the following string concatenation example.

var x = "Hello, world: " + (19 + 23);

cc @dotnet/roslyn-ide

@DustinCampbell
Copy link
Member Author

@Pilchie? @CyrusNajmabadi? @roslyn-ide?

@CyrusNajmabadi
Copy link
Member

Looking.

</code>

Await TestAsync(input, expected)
End Function

#End Region
Copy link
Member

Choose a reason for hiding this comment

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

why are there so many changes in this file?

Copy link
Member Author

Choose a reason for hiding this comment

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

The tests in this file had been organized at one point, but recently added tests weren't added with the same organization. I organized them.

Copy link
Member Author

Choose a reason for hiding this comment

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

IOW, I moved a bunch of tests around.

Copy link
Member Author

Choose a reason for hiding this comment

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

I marked the newly added tests. Sorry about the diff.

@CyrusNajmabadi
Copy link
Member

Code changes look good. Don't understand the test changes though...


[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsInlineTemporary)]
[WorkItem(11958, "https://github.com/dotnet/roslyn/issues/11958")]
public async Task EnsureParenthesesInStringConcatenation()
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's a new test

<Fact, Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestVisualBasic_DontRemoveParenthesesAroundXmlElementWhenPreviousTokenIsGreaterThan() As Task
Public Async Function TestCSharp_SimplifyIfBinaryExpressionTypeIsImplicitNumericConversion() As Task
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's a new test

<Fact, Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestVisualBasic_DontRemoveParenthesesAroundXmlElementWhenPreviousTokenIsLessThan() As Task
Public Async Function TestCSharp_SimplifyIfBinaryExpressionTypeIsIdentityConversion() As Task
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's a new test

<Fact, Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestVisualBasic_DontRemoveParenthesesAroundEmptyXmlElementWhenPreviousTokenIsGreaterThan() As Task
Public Async Function TestCSharp_DontSimplifyIfOperatorOverloadsWouldNoLongerByCalled() As Task
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's a new test

<Fact, Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestVisualBasic_DontRemoveParenthesesAroundEmptyXmlElementWhenPreviousTokenIsLessThan() As Task
Public Async Function TestCSharp_DontSimplifyIfItWouldChangeStringConcatenation() As Task
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's a new test

<Fact, Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestVisualBasic_RemoveParenthesesAroundXmlElementInInvocation() As Task
Public Async Function TestCSharp_SimplifyInStringConcatenationIfItWouldNotChangeMeaning2() As Task
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's a new test

<Fact, Trait(Traits.Feature, Traits.Features.Simplification)>
Public Async Function TestVisualBasic_RemoveParenthesesAroundXmlElement() As Task
Public Async Function TestCSharp_SimplifyInStringConcatenationIfItWouldNotChangeMeaning1() As Task
Copy link
Member Author

Choose a reason for hiding this comment

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

Here's a new test

@CyrusNajmabadi
Copy link
Member

👍

@DustinCampbell
Copy link
Member Author

Looking forward to getting some check marks so I can merge. 😄

Copy link
Member

@Pilchie Pilchie left a comment

Choose a reason for hiding this comment

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

Changes look good to me, but I wonder if this should target master instead of dev16?

@DustinCampbell
Copy link
Member Author

I have no idea what we're supposed to target any more. @jasonmalinowski sent mail about branches and then ret-con'd it, but it wasn't clear how much was ret-con'd. So, I don't know what to target.

@jasonmalinowski
Copy link
Member

master is safe to target, and is probably the right place to go. The only thing that was retconned was dev15.1.x.

@DustinCampbell
Copy link
Member Author

Well then, yes, it should be against master 😊

@DustinCampbell DustinCampbell changed the base branch from dev16 to master February 22, 2017 14:51
@DustinCampbell
Copy link
Member Author

retest this please

@DustinCampbell DustinCampbell merged commit 7a2d700 into dotnet:master Feb 22, 2017
@DustinCampbell DustinCampbell deleted the fix-remote-parentheses branch August 21, 2017 17:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants