-
Notifications
You must be signed in to change notification settings - Fork 6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
205 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
--- | ||
title: "CA1311: Specify a culture or use an invariant version (code analysis)" | ||
description: "Learn about code analysis rule CA1311: Specify a culture or use an invariant version" | ||
ms.date: 11/16/2022 | ||
ms.topic: reference | ||
f1_keywords: | ||
- CA1311 | ||
- SpecifyCultureForToLowerAndToUpper | ||
helpviewer_keywords: | ||
- CA1311 | ||
dev_langs: | ||
- CSharp | ||
- VB | ||
--- | ||
# CA1311: Specify a culture or use an invariant version | ||
|
||
| | Value | | ||
| ----------------------------------- | ------------------------------------------ | | ||
| **Rule ID** | CA1311 | | ||
| **Category** | [Globalization](globalization-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-breaking | | ||
|
||
## Cause | ||
|
||
A call is made to <xref:System.String.ToUpper?displayProperty=nameWithType> or <xref:System.String.ToLower?displayProperty=nameWithType> without specifying a culture. | ||
|
||
## Rule description | ||
|
||
Specify a culture or use an invariant culture to avoid implicit dependency on the current culture when calling `ToUpper` or `ToLower`. Using an invariant culture yields consistent results regardless of the culture of an application. | ||
|
||
## How to fix violations | ||
|
||
Instead of calling the parameterless <xref:System.String.ToUpper?displayProperty=nameWithType> or <xref:System.String.ToLower?displayProperty=nameWithType> methods, call <xref:System.String.ToUpper(System.Globalization.CultureInfo)> or <xref:System.String.ToUpperInvariant>, or <xref:System.String.ToLower(System.Globalization.CultureInfo)> or <xref:System.String.ToLowerInvariant>. | ||
|
||
## Example | ||
|
||
The following code snippet shows a violation of rule CA1311: | ||
|
||
```csharp | ||
string s = "hello"; | ||
s = s.ToLower(); | ||
``` | ||
|
||
```vb | ||
Dim s As String = "hello" | ||
s.ToLower() | ||
``` | ||
|
||
The following code snippet fixes the violation: | ||
|
||
```csharp | ||
string s = "hello"; | ||
s = s.ToLowerInvariant(); | ||
``` | ||
|
||
```vb | ||
Dim s As String = "hello" | ||
s.ToLowerInvariant() | ||
``` | ||
|
||
## When to suppress warnings | ||
|
||
It's safe to suppress a warning from this rule if you're certain that <xref:System.Threading.Thread.CurrentCulture%2A?displayProperty=nameWithType> will never change. | ||
|
||
## Suppress a warning | ||
|
||
If you just want to suppress a single violation, add preprocessor directives to your source file to disable and then re-enable the rule. | ||
|
||
```csharp | ||
#pragma warning disable CA1311 | ||
// The code that's violating the rule is on this line. | ||
#pragma warning restore CA1311 | ||
``` | ||
|
||
To disable the rule for a file, folder, or project, set its severity to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_diagnostic.CA1311.severity = none | ||
``` | ||
|
||
To disable this entire category of rules, set the severity for the category to `none` in the [configuration file](../configuration-files.md). | ||
|
||
```ini | ||
[*.{cs,vb}] | ||
dotnet_analyzer_diagnostic.category-Globalization.severity = none | ||
``` | ||
|
||
For more information, see [How to suppress code analysis warnings](../suppress-warnings.md). | ||
|
||
## See also | ||
|
||
- [Perform culture-insensitive case changes](../../../core/extensions/performing-culture-insensitive-case-changes.md) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
--- | ||
title: "CA1421: Method uses runtime marshalling when 'DisableRuntimeMarshallingAttribute' is applied" | ||
description: "Learn about code analysis rule CA1421: Method uses runtime marshalling when 'DisableRuntimeMarshallingAttribute' is applied" | ||
ms.date: 11/16/2022 | ||
ms.topic: reference | ||
f1_keywords: | ||
- CA1421 | ||
- MethodUsesRuntimeMarshallingEvenWhenMarshallingDisabled | ||
helpviewer_keywords: | ||
- CA1421 | ||
dev_langs: | ||
- CSharp | ||
- VB | ||
--- | ||
# CA1421: Method uses runtime marshalling when DisableRuntimeMarshallingAttribute is applied | ||
|
||
| | Value | | ||
| ----------------------------------- | ------------------------------------------------ | | ||
| **Rule ID** | CA1421 | | ||
| **Category** | [Interoperability](interoperability-warnings.md) | | ||
| **Fix is breaking or non-breaking** | Non-breaking | | ||
|
||
## Cause | ||
|
||
A method uses runtime marshalling, and runtime marshalling is explicitly disabled. | ||
|
||
## Rule description | ||
|
||
If a method uses runtime marshalling when runtime marshalling is disabled, it can cause unexpected behavior differences at run time due to different expectations of a type's native layout. | ||
|
||
## How to fix violations | ||
|
||
Enable runtime marshalling or use features like `sizeof` and pointers to ensure accurate results. | ||
|
||
## When to suppress warnings | ||
|
||
Don't suppress a warning from this rule. | ||
|
||
## Example | ||
|
||
The following code snippet shows a violation of CA1421: | ||
|
||
:::code language="csharp" source="snippets/csharp/extra-rules/ca1421.cs"::: | ||
:::code language="vb" source="snippets/vb/extra-rules/ca1421.vb"::: | ||
|
||
To fix the violation, remove the <xref:System.Runtime.CompilerServices.DisableRuntimeMarshallingAttribute> attribute on the assembly. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
17 changes: 17 additions & 0 deletions
17
docs/fundamentals/code-analysis/quality-rules/snippets/csharp/extra-rules/ca1421.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System.Runtime.CompilerServices; | ||
using System.Runtime.InteropServices; | ||
|
||
[assembly: DisableRuntimeMarshalling] | ||
|
||
class C | ||
{ | ||
public void Test() | ||
{ | ||
nint offset = Marshal.OffsetOf(typeof(ValueType), "field"); | ||
} | ||
} | ||
|
||
struct ValueType | ||
{ | ||
int field; | ||
} |
10 changes: 10 additions & 0 deletions
10
docs/fundamentals/code-analysis/quality-rules/snippets/csharp/extra-rules/extra-rules.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Library</OutputType> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<RootNamespace>extra_rules</RootNamespace> | ||
</PropertyGroup> | ||
|
||
</Project> | ||
|
15 changes: 15 additions & 0 deletions
15
docs/fundamentals/code-analysis/quality-rules/snippets/vb/extra-rules/ca1421.vb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
Imports System.Runtime.CompilerServices | ||
Imports System.Runtime.InteropServices | ||
|
||
<Assembly: DisableRuntimeMarshalling> | ||
|
||
Class C | ||
Shared Sub S1() | ||
Dim offset As IntPtr = Marshal.OffsetOf(GetType(ValueType), "field") | ||
End Sub | ||
End Class | ||
|
||
Structure ValueType | ||
Dim field As Integer | ||
End Structure |
10 changes: 10 additions & 0 deletions
10
docs/fundamentals/code-analysis/quality-rules/snippets/vb/extra-rules/extra-rules.vbproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<OutputType>Library</OutputType> | ||
<RootNamespace>extra_rules</RootNamespace> | ||
<TargetFramework>net7.0</TargetFramework> | ||
</PropertyGroup> | ||
|
||
</Project> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters