Skip to content

Commit b03a59d

Browse files
committed
Add IDisposable Analyzer and assess its usefulness. Fix some legitimate issues. Disable the analyzer due to numerous false positives.
1 parent 5539581 commit b03a59d

File tree

19 files changed

+431
-374
lines changed

19 files changed

+431
-374
lines changed

Rubberduck.API/VBA/Parser.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
using System.Linq;
66
using System.Runtime.InteropServices;
77
using System.Threading;
8-
using Rubberduck.Common;
98
using Rubberduck.Parsing.ComReflection;
109
using Rubberduck.Parsing.PreProcessing;
1110
using Rubberduck.Parsing.Rewriter;
1211
using Rubberduck.Parsing.Symbols.DeclarationLoaders;
1312
using Rubberduck.Parsing.VBA;
14-
using Rubberduck.Parsing.Symbols;
1513
using Rubberduck.Parsing.UIContext;
1614
using Rubberduck.Parsing.VBA.ComReferenceLoading;
1715
using Rubberduck.Parsing.VBA.DeclarationCaching;
@@ -69,10 +67,10 @@ public interface IParserEvents
6967
]
7068
public sealed class Parser : IParser, IDisposable
7169
{
72-
private RubberduckParserState _state;
73-
private SynchronousParseCoordinator _parser;
74-
private IVBE _vbe;
75-
private IVBEEvents _vbeEvents;
70+
private readonly RubberduckParserState _state;
71+
private readonly SynchronousParseCoordinator _parser;
72+
private readonly IVBE _vbe;
73+
private readonly IVBEEvents _vbeEvents;
7674
private readonly IUiDispatcher _dispatcher;
7775
private readonly CancellationTokenSource _tokenSource;
7876

@@ -235,6 +233,9 @@ public void Dispose()
235233
}
236234

237235
_disposed = true;
236+
_parser?.Dispose();
237+
_vbe?.Dispose();
238+
_tokenSource.Dispose();
238239
}
239240
}
240241
}

Rubberduck.CodeAnalysis/QuickFixes/IgnoreOnceQuickFix.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public override void Fix(IInspectionResult result)
3232

3333
int annotationLine;
3434
string codeLine;
35-
var component = _state.ProjectsProvider.Component(result.QualifiedSelection.QualifiedName);
35+
using (var component = _state.ProjectsProvider.Component(result.QualifiedSelection.QualifiedName))
3636
using (var module = component.CodeModule)
3737
{
3838
annotationLine = result.QualifiedSelection.Selection.StartLine;

Rubberduck.Core/Common/ExportFormatter.cs

Lines changed: 201 additions & 194 deletions
Large diffs are not rendered by default.

Rubberduck.Core/Common/WinAPI/RegistryAccess.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,11 @@ internal static RegistryKey GetDeviceKey(string device)
1818

1919
internal static string GetClassType(string classGuid)
2020
{
21-
var classGuidKey = Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\" + classGuid);
22-
23-
return classGuidKey != null ? (string)classGuidKey.GetValue("Class") : string.Empty;
21+
using (var classGuidKey =
22+
Registry.LocalMachine.OpenSubKey(@"SYSTEM\CurrentControlSet\Control\Class\" + classGuid))
23+
{
24+
return classGuidKey != null ? (string) classGuidKey.GetValue("Class") : string.Empty;
25+
}
2426
}
2527
}
2628
}

Rubberduck.Core/Common/WindowsOperatingSystem.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ public void ShowFolder(string folderPath)
1111
{
1212
Directory.CreateDirectory(folderPath);
1313
}
14-
Process.Start(folderPath);
14+
15+
using (Process.Start(folderPath)) { }
1516
}
1617
}
1718
}

Rubberduck.Core/UI/CodeExplorer/Commands/CopyResultsCommand.cs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,29 +22,31 @@ public CopyResultsCommand(RubberduckParserState state) : base(LogManager.GetCurr
2222

2323
protected override void OnExecute(object parameter)
2424
{
25-
const string XML_SPREADSHEET_DATA_FORMAT = "XML Spreadsheet";
25+
const string XML_SPREADSHEET_DATA_FORMAT = "XML Spreadsheet";
2626

27-
ColumnInfo[] ColumnInfos = { new ColumnInfo("Project"), new ColumnInfo("Folder"), new ColumnInfo("Component"), new ColumnInfo("Declaration Type"), new ColumnInfo("Scope"),
28-
new ColumnInfo("Name"), new ColumnInfo("Return Type") };
27+
ColumnInfo[] ColumnInfos = { new ColumnInfo("Project"), new ColumnInfo("Folder"), new ColumnInfo("Component"), new ColumnInfo("Declaration Type"), new ColumnInfo("Scope"),
28+
new ColumnInfo("Name"), new ColumnInfo("Return Type") };
2929

30-
// this.ProjectName, this.CustomFolder, this.ComponentName, this.DeclarationType.ToString(), this.Scope
31-
var aDeclarations = _state.AllUserDeclarations.Select(declaration => declaration.ToArray()).ToArray();
30+
// this.ProjectName, this.CustomFolder, this.ComponentName, this.DeclarationType.ToString(), this.Scope
31+
var aDeclarations = _state.AllUserDeclarations.Select(declaration => declaration.ToArray()).ToArray();
3232

33-
const string resource = "Rubberduck User Declarations - {0}";
34-
var title = string.Format(resource, DateTime.Now.ToString(CultureInfo.InvariantCulture));
35-
36-
var csvResults = ExportFormatter.Csv(aDeclarations, title, ColumnInfos);
37-
var htmlResults = ExportFormatter.HtmlClipboardFragment(aDeclarations, title, ColumnInfos);
38-
var rtfResults = ExportFormatter.RTF(aDeclarations, title);
33+
const string resource = "Rubberduck User Declarations - {0}";
34+
var title = string.Format(resource, DateTime.Now.ToString(CultureInfo.InvariantCulture));
35+
36+
var csvResults = ExportFormatter.Csv(aDeclarations, title, ColumnInfos);
37+
var htmlResults = ExportFormatter.HtmlClipboardFragment(aDeclarations, title, ColumnInfos);
38+
var rtfResults = ExportFormatter.RTF(aDeclarations, title);
3939

40-
var strm1 = ExportFormatter.XmlSpreadsheetNew(aDeclarations, title, ColumnInfos);
40+
using (var strm1 = ExportFormatter.XmlSpreadsheetNew(aDeclarations, title, ColumnInfos))
41+
{
4142
//Add the formats from richest formatting to least formatting
4243
_clipboard.AppendStream(DataFormats.GetDataFormat(XML_SPREADSHEET_DATA_FORMAT).Name, strm1);
4344
_clipboard.AppendString(DataFormats.Rtf, rtfResults);
4445
_clipboard.AppendString(DataFormats.Html, htmlResults);
4546
_clipboard.AppendString(DataFormats.CommaSeparatedValue, csvResults);
4647

4748
_clipboard.Flush();
49+
}
4850
}
4951
}
5052
}

Rubberduck.Core/UI/CodeExplorer/Commands/PrintCommand.cs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,23 @@ protected override void OnExecute(object parameter)
8787
{
8888
while (index < text.Count)
8989
{
90-
var font = new Font(new FontFamily("Consolas"), 10, FontStyle.Regular);
91-
printPageArgs.Graphics.DrawString(text[index++], font, Brushes.Black, 0, offsetY, new StringFormat());
90+
using (var font = new Font(new FontFamily("Consolas"), 10, FontStyle.Regular))
91+
using (var stringFormat = new StringFormat())
92+
{
93+
printPageArgs.Graphics.DrawString(text[index++], font, Brushes.Black, 0, offsetY,
94+
stringFormat);
9295

93-
offsetY += font.Height;
96+
offsetY += font.Height;
9497

95-
if (offsetY >= pageHeight)
96-
{
97-
printPageArgs.HasMorePages = true;
98-
offsetY = 0;
99-
return;
100-
}
98+
if (offsetY >= pageHeight)
99+
{
100+
printPageArgs.HasMorePages = true;
101+
offsetY = 0;
102+
return;
103+
}
101104

102-
printPageArgs.HasMorePages = false;
105+
printPageArgs.HasMorePages = false;
106+
}
103107
}
104108
};
105109

Rubberduck.Core/UI/Command/MenuItems/CommandBars/AppCommandBarBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ public virtual void Initialize()
8787

8888
try
8989
{
90+
Item?.Dispose();
9091
Item = Parent.Add(_name, _position);
9192
Item.IsVisible = true;
9293
}

Rubberduck.Core/UI/Command/MenuItems/ParentMenus/ParentMenuItemBase.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ public void Initialize()
7373
return;
7474
}
7575

76+
Item?.Dispose();
7677
Item = Parent.AddPopup(_beforeIndex);
7778

7879
Item.Tag = _key;

Rubberduck.Core/UI/UnitTesting/Commands/AddTestModuleCommand.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,12 @@ private string DeclarationFormatFor(string declarationFormat, string type, IUnit
125125

126126
private IVBProject GetProject()
127127
{
128-
var activeProject = _vbe.ActiveVBProject;
129-
if (!activeProject.IsWrappingNullReference)
130-
{
131-
return activeProject;
128+
using (var activeProject = _vbe.ActiveVBProject)
129+
{ if (!activeProject.IsWrappingNullReference)
130+
{
131+
return activeProject;
132+
}
132133
}
133-
134-
activeProject.Dispose();
135134

136135
using (var projects = _vbe.VBProjects)
137136
{
@@ -158,9 +157,11 @@ protected override void OnExecute(object parameter)
158157
{
159158
var parameterIsModuleDeclaration = parameter is ProceduralModuleDeclaration || parameter is ClassModuleDeclaration;
160159

161-
using (var project = parameter as IVBProject ??
162-
(parameterIsModuleDeclaration ? ((Declaration) parameter).Project : GetProject()))
160+
using (var activeProject = GetProject())
163161
{
162+
var project = parameter as IVBProject ??
163+
(parameterIsModuleDeclaration ? ((Declaration) parameter).Project : activeProject);
164+
164165
if (project == null || project.IsWrappingNullReference)
165166
{
166167
return;
@@ -197,7 +198,7 @@ protected override void OnExecute(object parameter)
197198
if (parameterIsModuleDeclaration)
198199
{
199200
var moduleCodeBuilder = new StringBuilder();
200-
var declarationsToStub = GetDeclarationsToStub((Declaration) parameter);
201+
var declarationsToStub = GetDeclarationsToStub((Declaration)parameter);
201202

202203
foreach (var declaration in declarationsToStub)
203204
{

0 commit comments

Comments
 (0)