CommunityToolkit / CommunityToolkit/dotnet

RelayCommand generates code that violates CA1708

Open
#1,045 2 comments 0 reactions 0 assignees View on GitHub
bug :bug:
Dominant language
C#
Stars
3.8k
Forks
400
PR merge metrics
No merged PRs in 30d

Description

### Describe the bug

RelayCommand generates code that violates [CA1708](https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca1708)

```
private global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand? editCommand;
public global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand EditCommand => editCommand ??= new global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand(new global::System.Func(EditAsync), CanEdit);
```

but should be:

```
private global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand? _editCommand;
public global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand EditCommand => _editCommand ??= new global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand(new global::System.Func(EditAsync), CanEdit);
```

### Regression

_No response_

### Steps to reproduce

Set latest-recommended on project

The error happens on net9 onwards since analysislevel_9_recommended.global has the rule

```
# CA1708: Identifiers should differ by more than case
dotnet_diagnostic.CA1708.severity = warning
```

```
[RelayCommand(CanExecute = nameof(CanEdit))]
private async Task EditAsync() {}
```

### Expected behavior

Generated code be like suggested in CA1708 description:

```
private global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand? _editCommand;
public global::CommunityToolkit.Mvvm.Input.IAsyncRelayCommand EditCommand => _editCommand ??= new global::CommunityToolkit.Mvvm.Input.AsyncRelayCommand(new global::System.Func(EditAsync), CanEdit);
```

### Screenshots

_No response_

### IDE and version

Rider

### IDE version

_No response_

### Nuget packages

- [ ] CommunityToolkit.Common
- [ ] CommunityToolkit.Diagnostics
- [ ] CommunityToolkit.HighPerformance
- [x] CommunityToolkit.Mvvm (aka MVVM Toolkit)

### Nuget package version(s)

8.4.0

### Additional context

_No response_

### Help us help you

Yes, but only if others can assist

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.