Repository navigation
Don't report CA1704 for EventArgs parameters named e - #85161
jasonmalinowski merged 2 commits into
Conversation
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
|
Azure Pipelines: Successfully started running 2 pipeline(s). There may be pipelines that require an authorized user to comment /azp run to run. |
|
@mavasani what do you think about that change? |
|
These names violate .net naming guidelines. It is unlikely we would take this. We would also need an issue filed first that we approved prior to accepting a pr to make a change like this. |
|
@CyrusNajmabadi Thanks, that's fair. There's more history here than I realised. On the issue requirement: dotnet/roslyn-analyzers#5183 already tracks this, still open and The The option defaults to Happy to close if you would rather not take it. |
|
@CyrusNajmabadi are there any chances to get this merged, too? |
|
I don't think it shoudl be merged, no. :) |
|
@CyrusNajmabadi although dotnet/roslyn-analyzers#5183 describes exactly this problem and developers will need to enable the behaviour? |
|
This post still reflects my feelings on it: #85161 (comment) |
) Fixes #85924 ### Problem `CA1704` reports the BCL's own multi-arity type parameter convention as an unmeaningful name: ```csharp public static int Combine<T1, T2>(T1 value1, T2 value2) => 0; // ~~ CA1704: On method 'C.Combine<T1, T2>(T1, T2)', consider // ~~ providing a more meaningful name than generic type // parameter name 'T1' ``` This is the shape used by Func<T1, T2>, Action<T1, T2>, Tuple<T1, T2> and HashCode.Combine<T1, …, T8>, so any code mirroring those signatures gets a diagnostic for every type parameter of every overload. Note also that plain T is not reported, so the rule accepts Combine<T> but rejects Combine<T1, T2>. ## Cause AnalyzeSymbol strips the conventional prefix before the check: ```csharp ReportDiagnosticsForSymbol(typeParameter, RemovePrefixIfPresent('T', typeParameter.Name), …); ``` so "T1" becomes "1" and "T" becomes "". In ReportDiagnosticsForSymbol, the spelling half then correctly ignores the result: ```csharp if (!IsWordAcronym(word) && !IsWordNumeric(word) && !IsWordSpelledCorrectly(word)) ``` but the unmeaningful half has no such guard and only tests the length: ```csharp if (checkForUnmeaningful && symbolName.Length == 1) ``` "1" has length 1, so it is reported — the diagnostic is even built from symbol.Name, which is why the message names a two character identifier 'T1'. ## Fix Reuse the exemption that is already there: ```csharp if (checkForUnmeaningful && symbolName.Length == 1 && !IsWordNumeric(symbolName)) ``` Because C# identifiers cannot start with a digit, symbolName can only be a digit when it came out of RemovePrefixIfPresent, i.e. for type parameters ('T' prefix) and interfaces ('I' prefix — an interface named I1 currently reports "more meaningful name than type name '1'"). Nothing else can reach the new condition. Unchanged: Method<TA> and class A {} are still reported, and no spelling behavior is touched. ## Tests Two additions, both asserting no diagnostics; the existing TA and A/B/C/D tests are untouched and act as the controls: - TypeTypeParameterNumeric_Verify_NoDiagnosticsAsync - MyClass<T1, T2>, MyStructure<T1>, IInterface<T1>, MyDelegate<T1> - MethodTypeParameterNumeric_Verify_NoDiagnosticsAsync - Method<T1, T2> Text.Analyzers.UnitTests passes (73/73). ## Notes for review - No new option: there is no scenario where reporting T1 is wanted, and gating the fix behind configuration would leave the false positive on by default. - No rule added, removed or re-leveled, so no AnalyzerReleases entry. - The change only removes diagnostics, so it cannot break existing builds. - Independent of #85161 (exclude_single_letter_parameters) - that one only affects SymbolKind.Parameter, this one only affects prefix-stripped names. They do touch the same if, so whichever merges second needs both clauses: ```csharp … && !IsWordNumeric(symbolName) && !IsExcludedSingleLetterParameter(symbol). ``` ###### Microsoft Reviewers: [Open in CodeFlow](https://microsoft.github.io/open-pr/?codeflow=https://github.com/dotnet/roslyn/pull/85162)
|
@JoeRobich thanks a lot for reviewing and approving #85162! Now that it's merged, I've updated this PR on top of it. Would you have time to take a look at this one as well? |
|
@akhera99 @jasonmalinowski Would you have time to take a look at this one as well? |
|
I agree with @CyrusNajmabadi I wouldn't want to take this PR as is, since one-letter variable names are kinda the main thing I'd expect this analyzer to flag. Is the specific concern always |
CA1704 reports every single-letter identifier as unmeaningful, which flags the conventional event handler signature (object sender, EventArgs e) and discard-style parameters. All 18 CA1704 descriptors share one diagnostic id and the unmeaningful check is not dictionary-driven, so today this can only be silenced by disabling the spell checker too. Adds the opt-in editorconfig option exclude_single_letter_parameters, mirroring exclude_single_letter_type_parameters for CA1715. Default behavior is unchanged.
431695b to
e0ff0ba
Compare
|
@jasonmalinowski thanks, that works for us. Reworked accordingly:
|
|
@CyrusNajmabadi Any thoughts on this? It's a bit of a special case hack, but given this pattern is quite common it seems reasonable to me. And I don't mind opening the door to a few other special cases here if they present themselves. |
CA1704 reports every single-letter identifier as unmeaningful, which flags the conventional event handler signature (object sender, EventArgs e). Legacy FxCop did not report a parameter named e whose type derives from System.EventArgs; this restores that exemption. Other single-letter names are still reported.
e0ff0ba to
eb8fa83
Compare
Same for me. Tnx. |
|
@CyrusNajmabadi @jasonmalinowski is there anything left to do on my side? |
|
@FreeAndNil nope! |
Related to dotnet/roslyn-analyzers#5183, follow-up to #85161 ### Problem `CA1704` reports `_` as an unmeaningful parameter name: ```csharp [ClassInitialize] public static void ClassInitialize(TestContext _) { } // ^ CA1704 ``` `_` is the established way to mark a parameter that a signature requires but the method does not use. IDE0060 (remove unused parameter) explicitly skips parameters named `_`, `_1`, … for that reason (`IsSymbolWithSpecialDiscardName`). So the two analyzers contradict each other: - keep `_` → CA1704 - rename to e.g. `context` → IDE0060 On one mid-size codebase, after #85161, 20 of the remaining 103 CA1704 findings were parameters named `_`. ## Change A parameter named `_` is no longer reported by CA1704. - `_1`, `__` etc. were never reported (longer than one character, no words to spell check) - fields named `_` are unaffected (the `_` prefix is stripped, nothing remains) - every other single-letter parameter is still reported ## Notes for review - Only removes diagnostics, so it cannot break existing builds. - No new rule, option or descriptor change, so no AnalyzerReleases entry is needed. - No new public API. ## Tests In `IdentifiersShouldBeSpelledCorrectlyTests`: - `_` as method, constructor and delegate parameter → no diagnostic
Related to dotnet/roslyn-analyzers#5183
Problem
CA1704(IdentifiersShouldBeSpelledCorrectly) does two things under one diagnostic id:it spell-checks identifiers against a dictionary, and it reports any identifier whose
name is a single character as "unmeaningful".
The second half fires on the standard event handler signature:
There is currently no way to keep the spell checker while opting out of this:
and #pragma warning disable CA1704 disable the spell checker as well;
e cannot be allow-listed.
This is also a regression from legacy FxCop, which did not report a parameter named
e whose type derives from System.EventArgs. The porting PR dotnet/roslyn-analyzers#1111
lists "Do not report lambda parameters or types deriving from System.EventArgs as
unmeaningful", but the EventArgs part never shipped.
Change
A parameter named e whose type is or derives from System.EventArgs is no longer
reported as unmeaningful. Following review feedback, this replaces the opt-in
editorconfig option this PR originally proposed.
Everything else is unaffected: other single-letter parameters (EventArgs a,
string e), types, members and type parameters are still reported, and the spell
check still runs on e.
Notes for review
Tests
In IdentifiersShouldBeSpelledCorrectlyTests:
Microsoft Reviewers: Open in CodeFlow