Skip to content

Commit 4c2d089

Browse files
authored
Merge branch 'next' into next
2 parents 2f1cb36 + 7af0c3e commit 4c2d089

File tree

3 files changed

+80
-28
lines changed

3 files changed

+80
-28
lines changed

RetailCoder.VBE/UI/Command/MenuItems/CommandBars/AppCommandBarBase.cs

Lines changed: 39 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
using System;
12
using System.Collections.Generic;
23
using System.Linq;
34
using System.Runtime.InteropServices;
@@ -38,14 +39,22 @@ public void Localize()
3839
return;
3940
}
4041

41-
foreach (var kvp in _items)
42+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
4243
{
43-
var item = kvp;
44-
UiDispatcher.Invoke(() =>
44+
try
4545
{
46-
item.Value.Caption = item.Key.Caption.Invoke();
47-
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
48-
});
46+
var item = kvp;
47+
UiDispatcher.Invoke(() =>
48+
{
49+
item.Value.Caption = item.Key.Caption.Invoke();
50+
item.Value.TooltipText = item.Key.ToolTipText.Invoke();
51+
});
52+
53+
}
54+
catch (Exception e)
55+
{
56+
Logger.Error(e, $"Assignment of {kvp.Value.GetType().Name}.Caption or .TooltipText for {kvp.Key.GetType().Name} threw an exception.");
57+
}
4958
}
5059
}
5160

@@ -60,7 +69,15 @@ public virtual void Initialize()
6069
Item.IsVisible = true;
6170
foreach (var item in _items.Keys.OrderBy(item => item.DisplayOrder))
6271
{
63-
_items[item] = InitializeChildControl(item);
72+
try
73+
{
74+
_items[item] = InitializeChildControl(item);
75+
76+
}
77+
catch (Exception e)
78+
{
79+
Logger.Error(e, $"Initialization of the menu item for {item.Command.GetType().Name} threw an exception.");
80+
}
6481
}
6582
}
6683

@@ -92,17 +109,23 @@ private ICommandBarControl InitializeChildControl(ICommandMenuItem item)
92109

93110
public void EvaluateCanExecute(RubberduckParserState state)
94111
{
95-
foreach (var kvp in _items)
112+
foreach (var kvp in _items.Where(kv => kv.Key != null && kv.Value != null))
96113
{
97114
var commandItem = kvp.Key;
98-
if (commandItem != null && kvp.Value != null)
115+
var canExecute = false;
116+
try
99117
{
100-
var canExecute = commandItem.EvaluateCanExecute(state);
101-
kvp.Value.IsEnabled = canExecute;
102-
if (commandItem.HiddenWhenDisabled)
103-
{
104-
kvp.Value.IsVisible = canExecute;
105-
}
118+
canExecute = commandItem.EvaluateCanExecute(state);
119+
120+
}
121+
catch (Exception e)
122+
{
123+
Logger.Error(e, $"{commandItem?.GetType().Name ?? nameof(ICommandMenuItem)}.EvaluateCanExecute(RubberduckParserState) threw an exception.");
124+
}
125+
kvp.Value.IsEnabled = canExecute;
126+
if (commandItem?.HiddenWhenDisabled ?? false)
127+
{
128+
kvp.Value.IsVisible = canExecute;
106129
}
107130
}
108131
}
@@ -144,21 +167,14 @@ private void RemoveChildren()
144167
_items.Clear();
145168
}
146169

147-
// note: HAAAAACK!!!
148-
private static int _lastHashCode;
149-
150170
private void child_Click(object sender, CommandBarButtonClickEventArgs e)
151171
{
152172
var item = _items.Select(kvp => kvp.Key).SingleOrDefault(menu => menu.GetType().FullName == e.Control.Tag);
153-
if (item == null || e.Control.Target.GetHashCode() == _lastHashCode)
173+
if (item == null)
154174
{
155175
return;
156176
}
157177

158-
// without this hack, handler runs once for each menu item that's hooked up to the command.
159-
// hash code is different on every frakkin' click. go figure. I've had it, this is the fix.
160-
_lastHashCode = e.Control.Target.GetHashCode();
161-
162178
Logger.Debug("({0}) Executing click handler for commandbar item '{1}', hash code {2}", GetHashCode(), e.Control.Caption, e.Control.Target.GetHashCode());
163179
item.Command.Execute(null);
164180
}

Rubberduck.Inspections/Concrete/ObjectVariableNotSetInspection.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,20 @@ protected override IEnumerable<IInspectionResult> DoGetInspectionResults()
3434

3535
var objectVariableNotSetReferences = referencesRequiringSetAssignment.Where(FlagIfObjectVariableNotSet);
3636

37-
return objectVariableNotSetReferences.Select(reference =>
37+
return objectVariableNotSetReferences
38+
.Select(reference =>
3839
new IdentifierReferenceInspectionResult(this,
39-
string.Format(InspectionsUI.ObjectVariableNotSetInspectionResultFormat, reference.Declaration.IdentifierName),
40-
State,
41-
reference));
40+
string.Format(InspectionsUI.ObjectVariableNotSetInspectionResultFormat, reference.Declaration.IdentifierName),
41+
State, reference));
4242
}
4343

4444
private bool FlagIfObjectVariableNotSet(IdentifierReference reference)
4545
{
46+
var allrefs = reference.Declaration.References;
4647
var letStmtContext = ParserRuleContextHelper.GetParent<VBAParser.LetStmtContext>(reference.Context);
47-
return (reference.IsAssignment && letStmtContext != null);
48+
49+
return reference.IsAssignment && (letStmtContext != null
50+
|| allrefs.Where(r => r.IsAssignment).All(r => ParserRuleContextHelper.GetParent<VBAParser.SetStmtContext>(r.Context)?.expression()?.GetText().Equals(Tokens.Nothing, StringComparison.InvariantCultureIgnoreCase) ?? false));
4851
}
4952
}
5053
}

RubberduckTests/Inspections/ObjectVariableNotSetInspectionTests.cs

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,39 @@ namespace RubberduckTests.Inspections
1111
[TestClass]
1212
public class ObjectVariableNotSetInspectionTests
1313
{
14+
[TestMethod]
15+
[TestCategory("Inspections")]
16+
public void ObjectVariableNotSet_OnlyAssignedToNothing_ReturnsResult()
17+
{
18+
var expectResultCount = 1;
19+
var input =
20+
@"
21+
Private Sub DoSomething()
22+
Dim target As Object
23+
target.DoSomething ' error 91
24+
Set target = Nothing
25+
End Sub
26+
";
27+
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
28+
}
29+
30+
[TestMethod]
31+
[TestCategory("Inspections")]
32+
public void ObjectVariableNotSet_AlsoAssignedToNothing_ReturnsNoResult()
33+
{
34+
var expectResultCount = 0;
35+
var input =
36+
@"
37+
Private Sub DoSomething()
38+
Dim target As Object
39+
Set target = New Object
40+
target.DoSomething
41+
Set target = Nothing
42+
End Sub
43+
";
44+
AssertInputCodeYieldsExpectedInspectionResultCount(input, expectResultCount);
45+
}
46+
1447
[TestMethod]
1548
[TestCategory("Inspections")]
1649
public void ObjectVariableNotSet_GivenIndexerObjectAccess_ReturnsNoResult()

0 commit comments

Comments
 (0)