You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -13,10 +13,10 @@ The documentation for Visual Basic and Visual C# are located in a separate repo
13
13
14
14
## Contributing to the documentation
15
15
16
-
To contribute to this documentation, please see the [Contributing guide](https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/CONTRIBUTING.md).
16
+
To contribute to this documentation, please see the [Contributing guide](CONTRIBUTING.md).
17
17
We welcome your contributions to help us improve the Visual Studio docs. All the articles in this repository use GitHub flavored markdown.
18
18
19
-
Several feature areas of Visual Studio have their own folders in this repo, such as **debugger** for topics on debugging, **ide** for topics on the Visual Studio interactive development environment (IDE), and so forth. The **/media** subfolder in each folder contains art files for the topics. The [Contributing guide](https://github.com/MicrosoftDocs/visualstudio-docs/blob/master/CONTRIBUTING.md) has more information.
19
+
Several feature areas of Visual Studio have their own folders in this repo, such as **debugger** for topics on debugging, **ide** for topics on the Visual Studio interactive development environment (IDE), and so forth. The **/media** subfolder in each folder contains art files for the topics. The [Contributing guide](CONTRIBUTING.md) has more information.
20
20
21
21
This project has adopted the [Microsoft Open Source Code of Conduct](https://opensource.microsoft.com/codeofconduct/). For more information see the [Code of Conduct FAQ](https://opensource.microsoft.com/codeofconduct/faq/) or contact [[email protected]](mailto:[email protected]) with any additional questions or comments.
Copy file name to clipboardExpand all lines: docs/code-quality/ca2104-do-not-declare-read-only-mutable-reference-types.md
+21-11Lines changed: 21 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: "CA2104: Do not declare read only mutable reference types"
3
-
ms.date: 11/04/2016
3
+
ms.date: 11/01/2018
4
4
ms.prod: visual-studio-dev15
5
5
ms.technology: vs-ide-code-analysis
6
6
ms.topic: reference
@@ -30,25 +30,35 @@ ms.workload:
30
30
|Category|Microsoft.Security|
31
31
|Breaking Change|Non-breaking|
32
32
33
+
> [!NOTE]
34
+
> Rule CA2104 is obsolete and will be removed in a future version of Visual Studio.
35
+
33
36
## Cause
34
-
An externally visible type contains an externally visible read-only field that is a mutable reference type.
37
+
38
+
An externally visible type contains an externally visible read-only field that is a mutable reference type.
35
39
36
40
## Rule description
37
-
A mutable type is a type whose instance data can be modified. The <xref:System.Text.StringBuilder?displayProperty=fullName> class is an example of a mutable reference type. It contains members that can change the value of an instance of the class. An example of an immutable reference type is the <xref:System.String?displayProperty=fullName> class. After it has been instantiated, its value can never change.
38
41
39
-
The read-only modifier ([readonly](/dotnet/csharp/language-reference/keywords/readonly) in C#, [ReadOnly](/dotnet/visual-basic/language-reference/modifiers/readonly) in [!INCLUDE[vbprvb](../code-quality/includes/vbprvb_md.md)], and [const](/cpp/cpp/const-cpp) in C++) on a reference type field (pointer in C++) prevents the field from being replaced by a different instance of the reference type. However, the modifier does not prevent the instance data of the field from being modified through the reference type.
42
+
A mutable type is a type whose instance data can be modified. The <xref:System.Text.StringBuilder?displayProperty=fullName> class is an example of a mutable reference type. It contains members that can change the value of an instance of the class. An example of an immutable reference type is the <xref:System.String?displayProperty=fullName> class. After it has been instantiated, its value can never change.
43
+
44
+
The read-only modifier ([readonly](/dotnet/csharp/language-reference/keywords/readonly) in C#, [ReadOnly](/dotnet/visual-basic/language-reference/modifiers/readonly) in Visual Basic, and [const](/cpp/cpp/const-cpp) in C++) on a reference type field (or pointer in C++) prevents the field from being replaced by a different instance of the reference type. However, the modifier does not prevent the instance data of the field from being modified through the reference type.
45
+
46
+
This rule may inadvertently show a violation for a type that is, in fact, immutable. In that case, it's safe to suppress the warning.
40
47
41
-
Read-only array fields are exempt from this rule but instead cause a violation of the [CA2105: Array fields should not be read only](../code-quality/ca2105-array-fields-should-not-be-read-only.md) rule.
48
+
Read-only array fields are exempt from this rule but instead cause a violation of the [CA2105: Array fields should not be read only](../code-quality/ca2105-array-fields-should-not-be-read-only.md) rule.
42
49
43
50
## How to fix violations
44
-
To fix a violation of this rule, remove the read-only modifier or, if a breaking change is acceptable, replace the field with an immutable type.
51
+
52
+
To fix a violation of this rule, remove the read-only modifier or, if a breaking change is acceptable, replace the field with an immutable type.
45
53
46
54
## When to suppress warnings
47
-
It is safe to suppress a warning from this rule if the field type is immutable.
55
+
56
+
It's safe to suppress a warning from this rule if the field type is immutable.
48
57
49
58
## Example
50
-
The following example shows a field declaration that causes a violation of this rule.
Copy file name to clipboardExpand all lines: docs/code-quality/code-metrics-values.md
+4-1Lines changed: 4 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
---
2
2
title: Calculate code metrics in Visual Studio
3
-
ms.date: 12/12/2017
3
+
ms.date: 11/02/2018
4
4
ms.prod: visual-studio-dev15
5
5
ms.technology: vs-ide-code-analysis
6
6
ms.topic: "conceptual"
@@ -34,6 +34,9 @@ The following list shows the code metrics results that Visual Studio calculates:
34
34
35
35
-**Lines of Code** - Indicates the approximate number of lines in the code. The count is based on the IL code and is therefore not the exact number of lines in the source code file. A very high count might indicate that a type or method is trying to do too much work and should be split up. It might also indicate that the type or method might be hard to maintain.
36
36
37
+
> [!NOTE]
38
+
> The [command-line version](../code-quality/how-to-generate-code-metrics-data.md#command-line-code-metrics) of the code metrics tool counts actual lines of code because it analyzes the source code instead of IL.
39
+
37
40
## Anonymous methods
38
41
39
42
An *anonymous method* is just a method that has no name. Anonymous methods are most frequently used to pass a code block as a delegate parameter. Metrics results for an anonymous method that is declared in a member, such as a method or accessor, are associated with the member that declares the method. They are not associated with the member that calls the method.
title: How to generate code metrics data in Visual Studio
3
-
ms.date: 12/12/2017
2
+
title: Generate code metrics from the IDE or command line
3
+
ms.date: 11/02/2018
4
4
ms.prod: visual-studio-dev15
5
5
ms.technology: vs-ide-code-analysis
6
6
ms.topic: "conceptual"
@@ -16,33 +16,149 @@ ms.workload:
16
16
---
17
17
# How to: Generate code metrics data
18
18
19
-
You can generate code metrics results for an entire solution or the selected project.
19
+
You can generate code metrics results for one or more projects or an entire solution. Code metrics is available within the Visual Studio interactive development environment (IDE) and, for C# and Visual Basic projects, at the command line.
20
20
21
-
## To generate code metrics results for an entire solution
21
+
In addition, you can install a [NuGet package](https://dotnet.myget.org/feed/roslyn-analyzers/package/nuget/Microsoft.CodeAnalysis.FxCopAnalyzers/2.6.2-beta2-63202-01) that includes four code metrics [analyzer](roslyn-analyzers-overview.md) rules: CA1501, CA1502, CA1505, and CA1506. These rules are disabled by default, but you can enable them from **Solution Explorer** or in a [rule set](using-rule-sets-to-group-code-analysis-rules.md) file.
22
22
23
-
- From the menu bar, choose **Analyze** > **Calculate Code Metrics** > **For Solution**.
23
+
## Visual Studio IDE code metrics
24
24
25
-
\- or -
25
+
### Generate code metrics results for an entire solution
26
26
27
-
- In **Solution Explorer**, right-click the solution and then choose **Calculate Code Metrics**.
27
+
You can generate code metrics results for an entire solution in any of the following ways:
28
+
29
+
- From the menu bar, choose **Analyze** > **Calculate Code Metrics** > **For Solution**.
28
30
29
-
\- or -
31
+
- In **Solution Explorer**, right-click the solution and then choose **Calculate Code Metrics**.
30
32
31
33
- In the **Code Metrics Results** window, choose the **Calculate Code Metrics for Solution** button.
32
34
33
-
The results are generated and the **Code Metrics Results** window is displayed.
35
+
The results are generated and the **Code Metrics Results** window is displayed. To view the results details, expand the tree in the **Hierarchy** column.
34
36
35
-
##To generate code metrics results for one or more selected projects
37
+
### Generate code metrics results for one or more projects
36
38
37
39
1. In **Solution Explorer**, select one or more projects.
38
40
39
-
1. From the menu bar, choose **Analyze** > **Calculate Code Metrics** > **For <project\>**.
40
-
41
-
The results are generated and the **Code Metrics Results** window is displayed.
42
-
43
-
## To view the results details
44
-
45
-
In the **Code Metrics Results** window, expand the tree in the **Hierarchy** column.
41
+
1. From the menu bar, choose **Analyze** > **Calculate Code Metrics** > **For Selected Project(s)**.
42
+
43
+
The results are generated and the **Code Metrics Results** window is displayed. To view the results details, expand the tree in the **Hierarchy**.
44
+
45
+
## Command-line code metrics
46
+
47
+
You can generate code metrics data from the command line for C# and Visual Basic projects for .NET Framework, .NET Core, and .NET Standard apps. The command line code metrics tools is called *Metrics.exe*.
48
+
49
+
To obtain the *Metrics.exe* executable, you must [generate it yourself](#generate-the-executable). In the near future, a [published version of *Metrics.exe* will be available](https://github.com/dotnet/roslyn-analyzers/issues/1756) so you don't have to build it yourself.
50
+
51
+
### Generate the executable
52
+
53
+
To generate the executable *Metrics.exe*, follow these steps:
54
+
55
+
1. Clone the [dotnet/roslyn-analyzers](https://github.com/dotnet/roslyn-analyzers) repo.
56
+
2. Open Developer Command Prompt for Visual Studio as an administrator.
57
+
3. From the root of the **roslyn-analyzers** repo, execute the following command: `Restore.cmd`
58
+
4. Change directory to *src\Tools*.
59
+
5. Execute the following command to build the **Metrics.csproj** project:
Previous versions of Visual Studio, including Visual Studio 2015, included a command-line code metrics tool called *Metrics.exe*. This previous version of the tool did a binary analysis, that is, an assembly-based analysis. The new tool analyzes source code instead. Because the new *Metrics.exe* is source code-based, the results are different to what's generated by previous versions of *Metrics.exe* and within the Visual Studio 2017 IDE.
144
+
145
+
The new *Metrics.exe* tool can compute metrics even in the presence of source code errors, as long as the solution and project can be loaded.
146
+
147
+
#### Metric value differences
148
+
149
+
The `LinesOfCode` metric is more accurate and reliable in the new *Metrics.exe*. It is independent of any codegen differences and doesn’t change when the toolset or runtime changes. The new *Metrics.exe* counts actual lines of code, including blank lines and comments.
150
+
151
+
Other metrics such as `CyclomaticComplexity` and `MaintainabilityIndex` use the same formulas as previous versions of *Metrics.exe*, but the new *Metrics.exe* counts the number of `IOperations` (logical source instructions) instead of intermediate language (IL) instructions. The numbers will be slightly different from previous versions of *Metrics.exe* and from the Visual Studio 2017 IDE code metrics results.
152
+
153
+
### Legacy mode
154
+
155
+
You can also choose to build *Metrics.exe* in *legacy mode*. The legacy mode version of the tool generates metric values that are closer to what older versions of the tool generated. Additionally, in legacy mode, *Metrics.exe* generates code metrics for the same set of method types that previous versions of the tool generated code metrics for. For example, it doesn't generate code metrics data for field and property initializers. Legacy mode is useful for backwards compatibility or if you have code check-in gates based on code metrics numbers. The command to build *Metrics.exe* in legacy mode is:
0 commit comments