Skip to content

Fix for RemoveRedundantToStringCall(RCS1097) #1037

Merged
josefpihrt merged 4 commits intodotnet:mainfrom
jamesHargreaves12:fix/RCS1097
Feb 26, 2023
Merged

Fix for RemoveRedundantToStringCall(RCS1097) #1037
josefpihrt merged 4 commits intodotnet:mainfrom
jamesHargreaves12:fix/RCS1097

Conversation

@jamesHargreaves12
Copy link
Copy Markdown
Contributor

Current behaviour:

  • RCS1097 suggest removing ToString for structs which can lead to boxing.

It looks as if this was attempted to be prevented by using an IsReferenceType check at:
https://github.com/JosefPihrt/Roslynator/blob/3c6f1c1fc24b63aea6b0d82e64ec4e02d9ca39ad/src/Analyzers/CSharp/Analysis/RemoveRedundantToStringCallAnalysis.cs#L60

Unfortunately, this doesn't work for structs as they have type System.ValueType which is considered a reference type. See
https://learn.microsoft.com/en-us/dotnet/api/system.type.isvaluetype?redirectedfrom=MSDN&view=net-7.0#System_Type_IsValueType

Fix implemented just explicitly checks for System_ValueType.

Also added a test to catch future regressions.

@josefpihrt
Copy link
Copy Markdown
Collaborator

Hi,

thanks for the PR, I just made some minor comments.

jamesHargreaves12 and others added 2 commits February 15, 2023 21:07
…sts.cs

Co-authored-by: Josef Pihrt <josef@pihrt.net>
…sts.cs

Co-authored-by: Josef Pihrt <josef@pihrt.net>
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.

2 participants