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

RelayCommand generates code that violates CA1708 #1045

Open
1 of 4 tasks
michaelmairegger opened this issue Jan 20, 2025 · 2 comments
Open
1 of 4 tasks

RelayCommand generates code that violates CA1708 #1045

michaelmairegger opened this issue Jan 20, 2025 · 2 comments
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior

Comments

@michaelmairegger
Copy link

michaelmairegger commented Jan 20, 2025

Describe the bug

RelayCommand generates code that violates 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<global::System.Threading.Tasks.Task>(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<global::System.Threading.Tasks.Task>(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<global::System.Threading.Tasks.Task>(EditAsync), CanEdit);

Screenshots

No response

IDE and version

Rider

IDE version

No response

Nuget packages

  • CommunityToolkit.Common
  • CommunityToolkit.Diagnostics
  • CommunityToolkit.HighPerformance
  • 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

@michaelmairegger michaelmairegger added the bug 🐛 An unexpected issue that highlights incorrect behavior label Jan 20, 2025
@bzd3y
Copy link

bzd3y commented Feb 26, 2025

Are you sure that is violating CA1708? I think, and the documentation seems to confirm, that by default it only looks at "externally visible" members, so it might not be checking that private field. Are you getting a warning for this or did you just notice that it violated the principle?

I'm also pretty sure the analyzers ignore generated code, but maybe I just remember discussing that at some point.

But I do agree that this violates the convention of private fields being prefixed with a _ and should be changed to do that.

@michaelmairegger
Copy link
Author

I got a warning, but today not anymore. even if I set the rule to dotnet_diagnostic.CA1708.severity=error

Yes, I agree, even generated code should follow the official guidelines.

Geat work so far on this project.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior
Projects
None yet
Development

No branches or pull requests

2 participants