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

Prevent Code Analysis to warn about "unused parameters" for operator weaving instruction #105

Merged
merged 13 commits into from
Aug 29, 2019

Conversation

BrunoJuchli
Copy link
Contributor

@BrunoJuchli BrunoJuchli commented Jul 31, 2019

Description

Version 3 brought a breaking change which requires operators to be defined in source code like:

public static bool operator ==(OnlyOperator left, OnlyOperator right) => Operator.Weave();
public static bool operator !=(OnlyOperator left, OnlyOperator right) => Operator.Weave();

This causes code analysis to warn about the equality operator's parameters (left, right) being unused (CA1801)

The solution

Replace Operator.Weave<T>() with Operator.Weave<T>(T left, T right).
Due to this being a breaking change, the major version is increased (V4).

Alternate Solutions
Support multiple Operator.Weave overloads

Instead of replacing Operator.Weave(), we could have introduced an additional Operator.Weave(left, right) for those who care about CA1801.

However, this would increase the required test and maintenance effort.

Migration: as long as the user has used invarying names for the parameters, just like left and right, he can perform a solution wide string replacement of Operator.Weave() by Operator.Weave(left, right).

throw exception

Adapt Operator.Weave to return an NotSupportedException / Exception. Adapt operators to throw:
public static bool operator ==(T left, T right) => throw Operator.Weave(left, right);
public static bool operator !=(T left, T right) => throw Operator.Weave(left, right);

This, however, also results in code analysis warnings (CA1605: op_Equality creates an exception of type *****. Exceptions should not be raised in this type of method. If this exception instance might be raised, change this method's logic so it no longer raises an exception.)

So not an improvement.

Todos

  • Related issues
  • Tests
  • Documentation

@BrunoJuchli
Copy link
Contributor Author

BrunoJuchli commented Aug 25, 2019

@SimonCropp
Could I get your opinion on:

Is it better to have a breaking change from:

public static bool operator ==(T left, T right) => Operator.Weave();

to

public static bool operator ==(T left, T right) => Operator.Weave(left, right);

OR

Support both, which increases test coverage and makes maintaining this harder?

I'd lean towards the breaking change. After all the breaking change can easily be resolved by performing a simple "replace all in solution".

@SimonCropp
Copy link
Member

@BrunoJuchli yep go for the breaking change :)

@BrunoJuchli BrunoJuchli marked this pull request as ready for review August 25, 2019 07:53
@BrunoJuchli
Copy link
Contributor Author

BrunoJuchli commented Aug 25, 2019

@SimonCropp
I've increased the version from 3.1 to 4.0 because of the breaking change. And I've documented the breaking change @ Readme.md.

I've also done a quick local test with the appveyor-built nuget package, and it worked as expected.

I hope it's good to merge & I'd recommend a squash commit.

@BrunoJuchli
Copy link
Contributor Author

@SimonCropp
Could you please release this? i.E. merge to master (squash commit?) & release package?

@SimonCropp SimonCropp merged commit 6f5a26f into Fody:master Aug 29, 2019
@SimonCropp SimonCropp added this to the 4.0.0 milestone Aug 29, 2019
@SimonCropp
Copy link
Member

sorry for the delay

This is now deployed. NuGet may take some time to make it available for download.

@ndrwrbgs
Copy link
Contributor

@BrunoJuchli were these warnings coming out during build? I'm curious if it's possible to direct StaticAnalysis to run AFTER Fody, and what the behavior of that would be.

I'm curious because I'm about to contribute to another project that replaces code with a pattern that involves throwing an exception to make sure things fail if the code replacement isn't done (to make it clearer to readers that the code they see isn't the code that will be run), and I don't want to just make the same problem you hit here worse for people with FxCop enabled.

@ndrwrbgs
Copy link
Contributor

As an aside, @BrunoJuchli it looks like an alternative might have been just renaming the arguments to == and != to _1 and _2, which are ignored for the purposes of the rule. This could have helped avoid needing a breaking change perhaps?

@BrunoJuchli
Copy link
Contributor Author

@ndrwrbgs
FxCop runs after Fody, but Roslyn based analyzers run before Fody. Running them after Fody would mean you'd have to create c# from IL and then run them on that c# code. No one's going to do that.

Also, I've experimented with discard parameter names and I couldn't get rid of the warnings.
Do you have a source for

(...) to _1 and _2, which are ignored for the purposes of the rule

?
It would have been great to avoid a breaking change.

@BrunoJuchli BrunoJuchli deleted the PreventCA1801ForOperators branch September 24, 2019 05:27
@ndrwrbgs
Copy link
Contributor

The CA1801 link you provide in the original post says it :)

@SimonCropp SimonCropp added the Bug label Jan 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants