« Code analysis » : différence entre les versions
De Banane Atomic
Aller à la navigationAller à la recherche
(Page créée avec « Category:.NET Core = [https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview?tabs=net-8 Code analysis] = * [https://editorconfig.org/ editorconfig] == Code quality analysis == Those [https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview?tabs=net-7#enabled-rules rules] are enabled by default.<br> Here is how to enabled [https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview?tabs=net-7#enable-a... ») |
|||
(21 versions intermédiaires par le même utilisateur non affichées) | |||
Ligne 1 : | Ligne 1 : | ||
[[Category:.NET Core]] | [[Category:.NET Core]] | ||
= [https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview?tabs=net-8 | = Links = | ||
* [https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview?tabs=net-8 .NET source code analysis] | |||
* [https://editorconfig.org/ editorconfig] | * [https://editorconfig.org/ editorconfig] | ||
= | = [https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview?tabs=net-8#enable-additional-rules Enable additional rules] = | ||
Those [https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/overview?tabs=net-8#enabled-rules rules] are enabled by default.<br> | |||
Edit the project file to enable additional rules: | |||
<filebox fn='MyProject.csproj' lang='xml'> | <filebox fn='MyProject.csproj' lang='xml'> | ||
<PropertyGroup> | <PropertyGroup> | ||
Ligne 13 : | Ligne 14 : | ||
</filebox> | </filebox> | ||
= Editorconfig = | |||
== Core options == | |||
<filebox fn='.editorconfig' lang='ini'> | |||
= | |||
<filebox fn='.editorconfig' lang='ini' | |||
root = true | root = true | ||
Ligne 71 : | Ligne 37 : | ||
indent_size = 4 | indent_size = 4 | ||
trim_trailing_whitespace = true | trim_trailing_whitespace = true | ||
</filebox> | |||
== Analyzer configuration == | |||
<filebox fn='.editorconfig' lang='ini'> | |||
# set the rules severity to warning | # set the rules severity to warning | ||
dotnet_analyzer_diagnostic.severity = warning | dotnet_analyzer_diagnostic.severity = warning | ||
</filebox> | |||
== Miscellaneous configuration == | |||
<filebox fn='.editorconfig' lang='ini'> | |||
# | # Sort using and Import directives with System.* appearing first | ||
dotnet_sort_system_directives_first = true | dotnet_sort_system_directives_first = true | ||
</filebox> | |||
== Style configuration == | |||
<filebox fn='.editorconfig' lang='ini'> | |||
# IDE0160 / IDE0161: Namespace declaration preferences | |||
csharp_style_namespace_declarations = file_scoped : error | |||
# | |||
# IDE0007 / IDE0008: 'var' preferences | # IDE0007 / IDE0008: 'var' preferences | ||
csharp_style_var_for_built_in_types = true | csharp_style_var_for_built_in_types = true | ||
csharp_style_var_when_type_is_apparent = true | csharp_style_var_when_type_is_apparent = true | ||
csharp_style_var_elsewhere = | csharp_style_var_elsewhere = false : suggestion | ||
</filebox> | |||
== Disable rules == | |||
<filebox fn='.editorconfig' lang='ini'> | |||
# lower speller rule severity to suggestion | |||
dotnet_diagnostic.VSSpell001.severity = suggestion | |||
dotnet_diagnostic.VSSpell002.severity = suggestion | |||
# | |||
dotnet_diagnostic. | |||
dotnet_diagnostic. | |||
# Specify CultureInfo | # Specify CultureInfo | ||
Ligne 135 : | Ligne 72 : | ||
# Specify a culture | # Specify a culture | ||
dotnet_diagnostic.CA1311.severity = suggestion | dotnet_diagnostic.CA1311.severity = suggestion | ||
</filebox> | |||
== Custom naming convention rules == | |||
<filebox fn='.editorconfig' lang='ini'> | |||
# Style Definitions | # Style Definitions | ||
dotnet_naming_style.upper_case_style.capitalization = all_upper | dotnet_naming_style.upper_case_style.capitalization = all_upper | ||
dotnet_naming_style.camel_case_style.capitalization = camel_case | dotnet_naming_style.camel_case_style.capitalization = camel_case | ||
dotnet_naming_style.end_with_async_style.required_suffix = Async | |||
dotnet_naming_style.end_with_async_style.capitalization = pascal_case | |||
# Use | # Use UPPER_CASE for constant fields | ||
dotnet_naming_symbols. | dotnet_naming_symbols.constant_field_symbol.applicable_kinds = field | ||
dotnet_naming_symbols. | dotnet_naming_symbols.constant_field_symbol.required_modifiers = const | ||
dotnet_naming_rule.constant_fields_should_be_upper_case.symbols = | dotnet_naming_rule.constant_fields_should_be_upper_case.symbols = constant_field_symbol | ||
dotnet_naming_rule.constant_fields_should_be_upper_case.style = upper_case_style | dotnet_naming_rule.constant_fields_should_be_upper_case.style = upper_case_style | ||
dotnet_naming_rule.constant_fields_should_be_upper_case.severity = warning | dotnet_naming_rule.constant_fields_should_be_upper_case.severity = warning | ||
# Use camelCase for variables | # Use camelCase for local variables, parameters, instance fields | ||
dotnet_naming_symbols. | dotnet_naming_symbols.local_parameter_field_symbol.applicable_kinds = local, parameter, field | ||
dotnet_naming_rule.should_be_camel_case.symbols = local_parameter_field_symbol | |||
dotnet_naming_rule.should_be_camel_case.style = camel_case_style | |||
dotnet_naming_rule.should_be_camel_case.severity = warning | |||
dotnet_naming_rule. | |||
dotnet_naming_rule. | |||
dotnet_naming_rule. | |||
# Async methods should have "Async" suffix | # Async methods should have "Async" suffix | ||
dotnet_naming_symbols.async_method_symbol.applicable_kinds = method | |||
dotnet_naming_symbols.async_method_symbol.required_modifiers = async | |||
dotnet_naming_rule.should_end_with_async.symbols = async_method_symbol | |||
dotnet_naming_symbols. | dotnet_naming_rule.should_end_with_async.style = end_with_async_style | ||
dotnet_naming_symbols. | dotnet_naming_rule.should_end_with_async.severity = warning | ||
</filebox> | </filebox> | ||
== | = [https://docs.sonarsource.com/sonarlint/visual-studio Sonar Lint] = | ||
<filebox fn=' | * [https://rules.sonarsource.com/csharp/ C# rules] | ||
" | <filebox fn='.editorconfig' lang='ini'> | ||
# S1135: Track uses of "TODO" tags | |||
dotnet_diagnostic.S1135.severity = none | |||
</filebox> | </filebox> | ||
== Visual Studio == | == Installation == | ||
=== [https://learn.microsoft.com/en-us/visualstudio/code-quality/configure-live-code-analysis-scope-managed-code?view=vs-2022#custom-analysis-scope Set the scope of live code analysis] | * Installation the extension for Visual Studio or VS Code | ||
= Visual Studio = | |||
== Change Code Style rules and generate a .editorconfig file == | |||
Tools → Options → Text Editor → C# → Code Style → General | |||
== [https://learn.microsoft.com/en-us/visualstudio/code-quality/configure-live-code-analysis-scope-managed-code?view=vs-2022#custom-analysis-scope Set the scope of live code analysis] == | |||
# Tools → Options | # Tools → Options | ||
# Text Editor → C# → Advanced | # Text Editor → C# → Advanced | ||
Ligne 188 : | Ligne 123 : | ||
## Show compiler errors and warnings for: Open documents | ## Show compiler errors and warnings for: Open documents | ||
== [https:// | == [https://learn.microsoft.com/en-us/visualstudio/ide/code-styles-and-code-cleanup?view=vs-2022#apply-code-styles Code Cleanup] == | ||
Right click on the solution → Analyze and Code Cleanup: | |||
* Run Code Cleanup (Profile X) | |||
* Configure Code Cleanup (configure the profiles) | |||
* | * Set Analysis scope → Entire solution | ||
{| class="wikitable wtp" | |||
! Fixer | |||
! Description | |||
|- | |||
| Apply conditional expression preferences || apply ternary conditional expression {{boxx|? :}} | |||
|} | |||
{{ | |||
}} | |||
Version du 24 avril 2024 à 14:21
Links
Enable additional rules
Those rules are enabled by default.
Edit the project file to enable additional rules:
MyProject.csproj |
<PropertyGroup> <!-- ... --> <AnalysisLevel>latest-recommended</AnalysisLevel> </PropertyGroup> |
Editorconfig
Core options
.editorconfig |
root = true # All files [*] indent_style = space insert_final_newline = true charset = utf-8 # XML project files [*.csproj] indent_size = 2 # JSON config files [*.json] indent_size = 2 # C# code files [*.cs] indent_size = 4 trim_trailing_whitespace = true |
Analyzer configuration
.editorconfig |
# set the rules severity to warning dotnet_analyzer_diagnostic.severity = warning |
Miscellaneous configuration
.editorconfig |
# Sort using and Import directives with System.* appearing first dotnet_sort_system_directives_first = true |
Style configuration
.editorconfig |
# IDE0160 / IDE0161: Namespace declaration preferences csharp_style_namespace_declarations = file_scoped : error # IDE0007 / IDE0008: 'var' preferences csharp_style_var_for_built_in_types = true csharp_style_var_when_type_is_apparent = true csharp_style_var_elsewhere = false : suggestion |
Disable rules
.editorconfig |
# lower speller rule severity to suggestion dotnet_diagnostic.VSSpell001.severity = suggestion dotnet_diagnostic.VSSpell002.severity = suggestion # Specify CultureInfo dotnet_diagnostic.CA1304.severity = suggestion # Specify a culture dotnet_diagnostic.CA1311.severity = suggestion |
Custom naming convention rules
.editorconfig |
# Style Definitions dotnet_naming_style.upper_case_style.capitalization = all_upper dotnet_naming_style.camel_case_style.capitalization = camel_case dotnet_naming_style.end_with_async_style.required_suffix = Async dotnet_naming_style.end_with_async_style.capitalization = pascal_case # Use UPPER_CASE for constant fields dotnet_naming_symbols.constant_field_symbol.applicable_kinds = field dotnet_naming_symbols.constant_field_symbol.required_modifiers = const dotnet_naming_rule.constant_fields_should_be_upper_case.symbols = constant_field_symbol dotnet_naming_rule.constant_fields_should_be_upper_case.style = upper_case_style dotnet_naming_rule.constant_fields_should_be_upper_case.severity = warning # Use camelCase for local variables, parameters, instance fields dotnet_naming_symbols.local_parameter_field_symbol.applicable_kinds = local, parameter, field dotnet_naming_rule.should_be_camel_case.symbols = local_parameter_field_symbol dotnet_naming_rule.should_be_camel_case.style = camel_case_style dotnet_naming_rule.should_be_camel_case.severity = warning # Async methods should have "Async" suffix dotnet_naming_symbols.async_method_symbol.applicable_kinds = method dotnet_naming_symbols.async_method_symbol.required_modifiers = async dotnet_naming_rule.should_end_with_async.symbols = async_method_symbol dotnet_naming_rule.should_end_with_async.style = end_with_async_style dotnet_naming_rule.should_end_with_async.severity = warning |
Sonar Lint
.editorconfig |
# S1135: Track uses of "TODO" tags dotnet_diagnostic.S1135.severity = none |
Installation
- Installation the extension for Visual Studio or VS Code
Visual Studio
Change Code Style rules and generate a .editorconfig file
Tools → Options → Text Editor → C# → Code Style → General
Set the scope of live code analysis
- Tools → Options
- Text Editor → C# → Advanced
- Run background code analysis for: Current document
- Show compiler errors and warnings for: Open documents
Code Cleanup
Right click on the solution → Analyze and Code Cleanup:
- Run Code Cleanup (Profile X)
- Configure Code Cleanup (configure the profiles)
- Set Analysis scope → Entire solution
Fixer | Description |
---|---|
Apply conditional expression preferences | apply ternary conditional expression ? : |