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

Improve CA1802 Documentation with Warnings About Potential Risks of Using const #44169

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

shethaadit
Copy link
Contributor

@shethaadit shethaadit commented Jan 7, 2025

Summary

This update enhances the documentation for CA1802 by:

  • Adding a warning about the risks of replacing static readonly with const for public and externally visible members.
  • Clarifying that const is safe for private members and generally safe for internal members unless exposed via InternalsVisibleTo or deployed separately.

Fixes #40827


Internal previews

📄 File 🔗 Preview link
docs/fundamentals/code-analysis/quality-rules/ca1802.md CA1802: Use Literals Where Appropriate

@shethaadit shethaadit requested review from gewarren and a team as code owners January 7, 2025 16:43
@dotnetrepoman dotnetrepoman bot added this to the January 2025 milestone Jan 7, 2025
@dotnet-policy-service dotnet-policy-service bot added dotnet-fundamentals/svc community-contribution Indicates PR is created by someone from the .NET community. labels Jan 7, 2025
Copy link
Contributor

@gewarren gewarren left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @shethaadit. @BillWagner could you review from a C# perspective?

@@ -49,6 +49,12 @@ To fix a violation of this rule, replace the `static` and `readonly` modifiers w

It is safe to suppress a warning from this rule, or disable the rule, if performance is not of concern.

> [!WARNING]
> For public or externally visible members, changing `static readonly` to `const` can lead to issues. `const` values are embedded in dependent assemblies at compile time, so changes in the library's value may not propagate, potentially causing errors. Suppress this rule if the value might change in the future.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
> For public or externally visible members, changing `static readonly` to `const` can lead to issues. `const` values are embedded in dependent assemblies at compile time, so changes in the library's value may not propagate, potentially causing errors. Suppress this rule if the value might change in the future.
> For public or externally visible members, changing `static readonly` to `const` can lead to issues. `const` values are embedded in dependent assemblies at compile time, so changes in the library's value might not propagate, potentially causing errors. If the value of your member might change in the future, suppress this rule.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community-contribution Indicates PR is created by someone from the .NET community. dotnet-fundamentals/svc
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CA1802 documentation needs to warn about the downsides
2 participants