Skip to content

Commit 768d787

Browse files
authored
Merge pull request #3215 from MicrosoftDocs/master
11/2 Publishing
2 parents 3c17988 + b2db9c5 commit 768d787

File tree

59 files changed

+315
-162
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+315
-162
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ The documentation for Visual Basic and Visual C# are located in a separate repo
1313

1414
## Contributing to the documentation
1515

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).
1717
We welcome your contributions to help us improve the Visual Studio docs. All the articles in this repository use GitHub flavored markdown.
1818

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.
2020

2121
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.
2222

docs/code-quality/ca2104-do-not-declare-read-only-mutable-reference-types.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: "CA2104: Do not declare read only mutable reference types"
3-
ms.date: 11/04/2016
3+
ms.date: 11/01/2018
44
ms.prod: visual-studio-dev15
55
ms.technology: vs-ide-code-analysis
66
ms.topic: reference
@@ -30,25 +30,35 @@ ms.workload:
3030
|Category|Microsoft.Security|
3131
|Breaking Change|Non-breaking|
3232

33+
> [!NOTE]
34+
> Rule CA2104 is obsolete and will be removed in a future version of Visual Studio.
35+
3336
## 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.
3539

3640
## 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.
3841

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.
4047

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.
4249

4350
## 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.
4553

4654
## 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.
4857

4958
## Example
50-
The following example shows a field declaration that causes a violation of this rule.
5159

52-
[!code-cpp[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/CPP/ca2104-do-not-declare-read-only-mutable-reference-types_1.cpp)]
53-
[!code-csharp[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/CSharp/ca2104-do-not-declare-read-only-mutable-reference-types_1.cs)]
54-
[!code-vb[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/VisualBasic/ca2104-do-not-declare-read-only-mutable-reference-types_1.vb)]
60+
The following example shows a field declaration that causes a violation of this rule:
61+
62+
[!code-cpp[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/CPP/ca2104-do-not-declare-read-only-mutable-reference-types_1.cpp)]
63+
[!code-csharp[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/CSharp/ca2104-do-not-declare-read-only-mutable-reference-types_1.cs)]
64+
[!code-vb[FxCop.Security.MutableReferenceTypes#1](../code-quality/codesnippet/VisualBasic/ca2104-do-not-declare-read-only-mutable-reference-types_1.vb)]

docs/code-quality/code-metrics-values.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
22
title: Calculate code metrics in Visual Studio
3-
ms.date: 12/12/2017
3+
ms.date: 11/02/2018
44
ms.prod: visual-studio-dev15
55
ms.technology: vs-ide-code-analysis
66
ms.topic: "conceptual"
@@ -34,6 +34,9 @@ The following list shows the code metrics results that Visual Studio calculates:
3434

3535
- **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.
3636

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+
3740
## Anonymous methods
3841

3942
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.

docs/code-quality/how-to-generate-code-metrics-data.md

Lines changed: 133 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
---
2-
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
44
ms.prod: visual-studio-dev15
55
ms.technology: vs-ide-code-analysis
66
ms.topic: "conceptual"
@@ -16,33 +16,149 @@ ms.workload:
1616
---
1717
# How to: Generate code metrics data
1818

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.
2020

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.
2222

23-
- From the menu bar, choose **Analyze** > **Calculate Code Metrics** > **For Solution**.
23+
## Visual Studio IDE code metrics
2424

25-
\- or -
25+
### Generate code metrics results for an entire solution
2626

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**.
2830

29-
\- or -
31+
- In **Solution Explorer**, right-click the solution and then choose **Calculate Code Metrics**.
3032

3133
- In the **Code Metrics Results** window, choose the **Calculate Code Metrics for Solution** button.
3234

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.
3436

35-
## To generate code metrics results for one or more selected projects
37+
### Generate code metrics results for one or more projects
3638

3739
1. In **Solution Explorer**, select one or more projects.
3840

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:
60+
61+
```shell
62+
msbuild /m /v:m /p:Configuration=Release Metrics.csproj
63+
```
64+
65+
An executable named *Metrics.exe* is generated in the *Binaries* directory under the repo root.
66+
67+
> [!TIP]
68+
> To build *Metrics.exe* in [legacy mode](#legacy-mode), execute the following command:
69+
>
70+
> ```shell
71+
> msbuild /m /v:m /t:rebuild /p:LEGACY_CODE_METRICS_MODE=true Metrics.csproj
72+
> ```
73+
74+
### Usage
75+
76+
To run *Metrics.exe*, supply a project or solution and an output XML file as arguments. For example:
77+
78+
```shell
79+
C:\>Metrics.exe /project:ConsoleApp20.csproj /out:report.xml
80+
Loading ConsoleApp20.csproj...
81+
Computing code metrics for ConsoleApp20.csproj...
82+
Writing output to 'report.xml'...
83+
Completed Successfully.
84+
```
85+
86+
### Output
87+
88+
The generated XML output takes the following format:
89+
90+
```xml
91+
<?xml version="1.0" encoding="utf-8"?>
92+
<CodeMetricsReport Version="1.0">
93+
<Targets>
94+
<Target Name="ConsoleApp20.csproj">
95+
<Assembly Name="ConsoleApp20, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null">
96+
<Metrics>
97+
<Metric Name="MaintainabilityIndex" Value="100" />
98+
<Metric Name="CyclomaticComplexity" Value="1" />
99+
<Metric Name="ClassCoupling" Value="1" />
100+
<Metric Name="DepthOfInheritance" Value="1" />
101+
<Metric Name="LinesOfCode" Value="11" />
102+
</Metrics>
103+
<Namespaces>
104+
<Namespace Name="ConsoleApp20">
105+
<Metrics>
106+
<Metric Name="MaintainabilityIndex" Value="100" />
107+
<Metric Name="CyclomaticComplexity" Value="1" />
108+
<Metric Name="ClassCoupling" Value="1" />
109+
<Metric Name="DepthOfInheritance" Value="1" />
110+
<Metric Name="LinesOfCode" Value="11" />
111+
</Metrics>
112+
<Types>
113+
<NamedType Name="Program">
114+
<Metrics>
115+
<Metric Name="MaintainabilityIndex" Value="100" />
116+
<Metric Name="CyclomaticComplexity" Value="1" />
117+
<Metric Name="ClassCoupling" Value="1" />
118+
<Metric Name="DepthOfInheritance" Value="1" />
119+
<Metric Name="LinesOfCode" Value="7" />
120+
</Metrics>
121+
<Members>
122+
<Method Name="void Program.Main(string[] args)" File="C:\Users\mavasani\source\repos\ConsoleApp20\ConsoleApp20\Program.cs" Line="7">
123+
<Metrics>
124+
<Metric Name="MaintainabilityIndex" Value="100" />
125+
<Metric Name="CyclomaticComplexity" Value="1" />
126+
<Metric Name="ClassCoupling" Value="1" />
127+
<Metric Name="LinesOfCode" Value="4" />
128+
</Metrics>
129+
</Method>
130+
</Members>
131+
</NamedType>
132+
</Types>
133+
</Namespace>
134+
</Namespaces>
135+
</Assembly>
136+
</Target>
137+
</Targets>
138+
</CodeMetricsReport>
139+
```
140+
141+
### Tool differences
142+
143+
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:
156+
157+
```shell
158+
msbuild /m /v:m /t:rebuild /p:LEGACY_CODE_METRICS_MODE=true Metrics.csproj
159+
```
160+
161+
For more information, see [Enable generating code metrics in legacy mode](https://github.com/dotnet/roslyn-analyzers/pull/1841).
46162

47163
## See also
48164

0 commit comments

Comments
 (0)