Skip to content

Don't report CA1704 for EventArgs parameters named e - #85161

Merged
jasonmalinowski merged 2 commits into
dotnet:mainfrom
komsa:Feature/85161-ca1704-exclude-single-letter-parameters
Nov 2, 2026
Merged

jasonmalinowski merged 2 commits into
dotnet:mainfrom
komsa:Feature/85161-ca1704-exclude-single-letter-parameters

Conversation

@FreeAndNil

@FreeAndNil FreeAndNil commented Sep 4, 2026 •

Copy link
Copy Markdown
Contributor

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:

private void OnListChanged(object? sender, ListChangedEventArgs e) { }
//                                                              ^ CA1704

There is currently no way to keep the spell checker while opting out of this:

  • all 18 descriptors share the id CA1704, so dotnet_diagnostic.CA1704.severity
    and #pragma warning disable CA1704 disable the spell checker as well;
  • a custom CodeAnalysisDictionary.xml / .dic only affects the spelling half -
    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

  • 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:

  • e of type EventArgs, CancelEventArgs and a custom subclass → no diagnostic
  • EventArgs a → still reported
  • string e → still reported
Microsoft Reviewers: Open in CodeFlow

@dotnet-policy-service dotnet-policy-service Bot added the Community The pull request was submitted by a contributor who is not a Microsoft employee. label Sep 4, 2026
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

@FreeAndNil
FreeAndNil marked this pull request as ready for review September 4, 2026 05:58
@FreeAndNil
FreeAndNil requested a review from a team as a code owner September 4, 2026 05:58
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
Successfully started running 2 pipeline(s).
There may be pipelines that require an authorized user to comment /azp run to run.

@FreeAndNil

Copy link
Copy Markdown
Contributor Author

@mavasani what do you think about that change?

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

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.

@FreeAndNil

Copy link
Copy Markdown
Contributor Author

@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
labelled help wanted. mavasani's suggestion there in 2021 was an editorconfig option modelled
on CA1715's exclude_single_letter_type_parameters, which is what this PR implements.

The e case also looks more like a regression than a guideline violation. Legacy FxCop did not
report a parameter named e whose type derives from System.EventArgs, and the porting PR
dotnet/roslyn-analyzers#1111 has "Do not report lambda parameters or types deriving from
System.EventArgs as unmeaningful" ticked off. It never made it into the shipped analyzer: there
is no EventArgs handling in IdentifiersShouldBeSpelledCorrectly.cs at all.

The option defaults to false, so nothing changes unless someone opts in. It matters because all
18 CA1704 descriptors share one id, so today you cannot drop these findings and keep the
dictionary spell check.

Happy to close if you would rather not take it.

@FreeAndNil

Copy link
Copy Markdown
Contributor Author

@CyrusNajmabadi are there any chances to get this merged, too?

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

I don't think it shoudl be merged, no. :)

@FreeAndNil

Copy link
Copy Markdown
Contributor Author

@CyrusNajmabadi although dotnet/roslyn-analyzers#5183 describes exactly this problem and developers will need to enable the behaviour?
dotnet/roslyn-analyzers#5183 (comment)

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

This post still reflects my feelings on it: #85161 (comment)

JoeRobich pushed a commit that referenced this pull request Oct 26, 2026
)

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)
@FreeAndNil

Copy link
Copy Markdown
Contributor Author

@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?

@FreeAndNil

Copy link
Copy Markdown
Contributor Author

@akhera99 @jasonmalinowski Would you have time to take a look at this one as well?

@jasonmalinowski

Copy link
Copy Markdown
Member

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 EventArgs e parameters? Because I'd much be OK with just special-casing that in the analyzer given it is a widespread pattern across .NET.

@jasonmalinowski jasonmalinowski self-assigned this Oct 28, 2026
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.
@FreeAndNil
FreeAndNil force-pushed the Feature/85161-ca1704-exclude-single-letter-parameters branch from 431695b to e0ff0ba Compare October 29, 2026 21:49
@FreeAndNil

Copy link
Copy Markdown
Contributor Author

@jasonmalinowski thanks, that works for us. Reworked accordingly:

  • option removed
  • parameter e whose type is or derives from System.EventArgs no longer reported as unmeaningful (matches legacy FxCop)
  • other single-letter names still reported, incl. EventArgs a and string e
  • spell check unchanged

@FreeAndNil FreeAndNil changed the title Add exclude_single_letter_parameters option to CA1704 Don't report CA1704 for EventArgs parameters named e Oct 29, 2026
@jasonmalinowski

Copy link
Copy Markdown
Member

@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.
@FreeAndNil
FreeAndNil force-pushed the Feature/85161-ca1704-exclude-single-letter-parameters branch from e0ff0ba to eb8fa83 Compare October 29, 2026 23:09
@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

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.

Same for me. Tnx.

@FreeAndNil

Copy link
Copy Markdown
Contributor Author

@CyrusNajmabadi @jasonmalinowski is there anything left to do on my side?

@CyrusNajmabadi

Copy link
Copy Markdown
Contributor

@FreeAndNil nope!

@jasonmalinowski
jasonmalinowski merged commit bb732ca into dotnet:main Nov 2, 2026
23 checks passed
@dotnet-policy-service dotnet-policy-service Bot added this to the Next milestone Nov 2, 2026
@FreeAndNil
FreeAndNil deleted the Feature/85161-ca1704-exclude-single-letter-parameters branch November 2, 2026 21:07
jasonmalinowski added a commit that referenced this pull request Nov 3, 2026
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
@akhera99 akhera99 modified the milestones: Next, 18.14 Nov 20, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Community The pull request was submitted by a contributor who is not a Microsoft employee.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants