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: add modifying nested struct diagnostic #551

Merged
merged 1 commit into from
Aug 8, 2023

Conversation

TimothyMakkison
Copy link
Collaborator

@TimothyMakkison TimothyMakkison commented Jul 9, 2023

Add diagnostic for modifying a temporary value type

Description

Added check for if a nested value type is being modified.

Probably not a common issue so I went with a diagnostic.
I considered nested chain assignment or a with expression ie target.NestedValue = target.NestedValue with { StringValue = src.StringValue }; (can repeatedly chain)

Fixes #516

Checklist

  • The existing code style is followed
  • The commit message follows our guidelines
  • Performed a self-review of my code
  • Hard-to-understand areas of my code are commented
  • The documentation is updated (as applicable)
  • Unit tests are added/updated
  • Integration tests are added/updated (as applicable, especially if feature/bug depends on roslyn or framework version in use)

@codecov
Copy link

codecov bot commented Jul 25, 2023

Codecov Report

Merging #551 (c15c130) into main (3ee2e96) will increase coverage by 0.05%.
The diff coverage is 100.00%.

@@            Coverage Diff             @@
##             main     #551      +/-   ##
==========================================
+ Coverage   90.89%   90.94%   +0.05%     
==========================================
  Files         172      172              
  Lines        6106     6141      +35     
  Branches      777      782       +5     
==========================================
+ Hits         5550     5585      +35     
  Misses        378      378              
  Partials      178      178              
Files Changed Coverage Δ
...pingBodyBuilders/ObjectMemberMappingBodyBuilder.cs 100.00% <100.00%> (ø)
...Riok.Mapperly/Diagnostics/DiagnosticDescriptors.cs 100.00% <100.00%> (ø)

📣 We’re building smart automated test selection to slash your CI/CD build times. Learn more

@TimothyMakkison TimothyMakkison force-pushed the mapstruct_516 branch 3 times, most recently from eaa0cf4 to cd933e7 Compare July 28, 2023 14:07
@TimothyMakkison
Copy link
Collaborator Author

TimothyMakkison commented Jul 28, 2023

Sorry, I though I had fixed all of this a week ago. Turns out I forgot to push it 😅

  • fixed indentation
  • use correct bool
  • update test to verify mapping is not generated
  • use indexer instead of linq
  • added support for struct fields

This only checks the second last item, but couldn't be any in the path except for the last item be a problem?

Yeah, I'm still not sure about this. I decided that the only the last two values mattered.. Even if all the preceeding members are structs, the error only occurs when trying to set the final struct property. If the penultimate member is a class the error won't occur.

Cone to think of it, this probably isn't true when accessing fields

@TimothyMakkison TimothyMakkison force-pushed the mapstruct_516 branch 2 times, most recently from 3f3ed6d to 0941b0f Compare July 28, 2023 14:12
@TimothyMakkison TimothyMakkison marked this pull request as draft July 28, 2023 14:18
@latonz
Copy link
Contributor

latonz commented Jul 28, 2023

@TimothyMakkison ah yep on a second thought I think that makes sense, that only the second last is checked.

@TimothyMakkison TimothyMakkison force-pushed the mapstruct_516 branch 3 times, most recently from 151bf56 to 3ead800 Compare July 29, 2023 09:51
@TimothyMakkison TimothyMakkison marked this pull request as ready for review July 29, 2023 09:52
@TimothyMakkison
Copy link
Collaborator Author

Added CS1612 to error message.

Modified the check to iterate backwards, checking for value type properties. Checking the penultimate member would work if field types didn't exist

latonz
latonz previously approved these changes Aug 2, 2023
@TimothyMakkison
Copy link
Collaborator Author

TimothyMakkison commented Aug 8, 2023

Analyzer doesn't like the length of ValidateMappingSpecification. I've extracted the struct method but it's still too long.
Might be a bit controversial but I've temporarily disabled the warning. I'd argue a a long method with simple checks is easier to understand than one method calling 6 simple methods. Especially when the bulk of the length are arguments placed on new lines.
WDYT?

@TimothyMakkison TimothyMakkison force-pushed the mapstruct_516 branch 2 times, most recently from d01f886 to e0c6189 Compare August 8, 2023 19:17
@latonz
Copy link
Contributor

latonz commented Aug 8, 2023

IMO its okay here to ignore the warning, could you change it to suppressmessage attribute? Often the readability is improved that when reading a method and submethods are called instead of inlined code, code which I'm not interested in reading can be skipped easier.

@TimothyMakkison
Copy link
Collaborator Author

IMO its okay here to ignore the warning, could you change it to suppressmessage attribute?

Added attribute

Often the readability is improved that when reading a method and submethods are called instead of inlined code, code which I'm not interested in reading can be skipped easier.

Should I extract all the methods?

@latonz latonz enabled auto-merge (squash) August 8, 2023 19:53
@latonz latonz merged commit 1677632 into riok:main Aug 8, 2023
16 checks passed
@latonz
Copy link
Contributor

latonz commented Aug 8, 2023

Nope, should be fine like this, just my general opinion on the topic…

@github-actions
Copy link

🎉 This PR is included in version 3.1.0-next.2 🎉

The release is available on:

Your semantic-release bot 📦🚀

@github-actions
Copy link

🎉 This PR is included in version 3.1.0 🎉

The release is available on:

Your semantic-release bot 📦🚀

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.

MapProperty modifies temporary struct members
2 participants