Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

952 fix code scanning alert useless assignment to local variable #953

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ private bool CheckAccess()
public virtual void BeginOnUIThread(System.Action action)
{
ValidateDispatcher();
var dummy = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
_ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ public override string ToString()
if (count == 0)
return "";
StringBuilder sb = new StringBuilder();
var keys = this.Keys;
foreach (var key in this.Keys)
{
sb.AppendFormat("{0}={1}&", key, this[key]);
Expand Down
29 changes: 9 additions & 20 deletions src/Caliburn.Micro.Platform/Platforms/uap/AppManifestHelper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//
// Copyright (c) 2012 Tim Heuer
// Copyright (c) 2012 Tim Heuer
//
// Licensed under the Microsoft Public License (Ms-PL) (the "License");
// you may not use this file except in compliance with the License.
Expand All @@ -12,7 +11,6 @@
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

using System;
using System.Globalization;
Expand Down Expand Up @@ -49,8 +47,8 @@ public async static Task<VisualElement> GetManifestVisualElementsAsync()
SmallLogoUri = new Uri(string.Format("ms-appx:///{0}", vel.Attribute("Square30x30Logo").Value.Replace(@"\", @"/"))),
BackgroundColorAsString = vel.Attribute("BackgroundColor").Value
}).FirstOrDefault();
if (visualElementNode == null)

if (visualElementNode == null)
throw new ArgumentNullException("Could not parse the VisualElements from the app manifest.");

return visualElementNode;
Expand Down Expand Up @@ -79,7 +77,7 @@ private static Windows.UI.Color ToColor(string hexValue)
// in order to prevent parsing failures
if (String.Equals(hexValue, "transparent", StringComparison.OrdinalIgnoreCase))
return Windows.UI.Colors.Transparent;

hexValue = hexValue.Replace("#", string.Empty);

// some loose validation (not bullet-proof)
Expand All @@ -88,23 +86,14 @@ private static Windows.UI.Color ToColor(string hexValue)
throw new ArgumentException("This does not appear to be a proper hex color number");
}

byte a = 255;
byte r = 255;
byte g = 255;
byte b = 255;
byte a = (hexValue.Length == 8) ? byte.Parse(hexValue.Substring(0, 2), NumberStyles.HexNumber) : (byte)255;

int startPosition = 0;

// the case where alpha is provided
if (hexValue.Length == 8)
{
a = byte.Parse(hexValue.Substring(0, 2), NumberStyles.HexNumber);
startPosition = 2;
}
int startPosition = (hexValue.Length == 8) ? 2 : 0;

r = byte.Parse(hexValue.Substring(startPosition, 2), NumberStyles.HexNumber);
g = byte.Parse(hexValue.Substring(startPosition + 2, 2), NumberStyles.HexNumber);
b = byte.Parse(hexValue.Substring(startPosition + 4, 2), NumberStyles.HexNumber);
byte r = byte.Parse(hexValue.Substring(startPosition, 2), NumberStyles.HexNumber);
byte g = byte.Parse(hexValue.Substring(startPosition + 2, 2), NumberStyles.HexNumber);
byte b = byte.Parse(hexValue.Substring(startPosition + 4, 2), NumberStyles.HexNumber);

return Windows.UI.Color.FromArgb(a, r, g, b);
}
Expand Down
67 changes: 44 additions & 23 deletions src/Caliburn.Micro.Platform/XamlPlatformProvider.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
namespace Caliburn.Micro {
namespace Caliburn.Micro
{
using System;
using System.Collections.Generic;
using System.Threading;
Expand All @@ -19,7 +20,8 @@
/// <summary>
/// A <see cref="IPlatformProvider"/> implementation for the XAML platfrom.
/// </summary>
public class XamlPlatformProvider : IPlatformProvider {
public class XamlPlatformProvider : IPlatformProvider
{
#if WINDOWS_UWP
private readonly CoreDispatcher dispatcher;
#else
Expand All @@ -29,7 +31,8 @@
/// <summary>
/// Initializes a new instance of the <see cref="XamlPlatformProvider"/> class.
/// </summary>
public XamlPlatformProvider() {
public XamlPlatformProvider()
{
#if WINDOWS_UWP
dispatcher = Window.Current.Dispatcher;
#elif AVALONIA
Expand All @@ -47,16 +50,19 @@
/// <summary>
/// Indicates whether or not the framework is in design-time mode.
/// </summary>
public virtual bool InDesignMode {
public virtual bool InDesignMode
{
get { return View.InDesignMode; }
}

private void ValidateDispatcher() {
private void ValidateDispatcher()
{
if (dispatcher == null)
throw new InvalidOperationException("Not initialized with dispatcher.");
}

private bool CheckAccess() {
private bool CheckAccess()
{
#if WINDOWS_UWP
return dispatcher == null || Window.Current != null;
#else
Expand All @@ -68,10 +74,11 @@
/// Executes the action on the UI thread asynchronously.
/// </summary>
/// <param name="action">The action to execute.</param>
public virtual void BeginOnUIThread(System.Action action) {
public virtual void BeginOnUIThread(System.Action action)
{
ValidateDispatcher();
#if WINDOWS_UWP
var dummy = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
_ = dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action());
#elif AVALONIA
dispatcher.Post(action);
#else
Expand All @@ -84,7 +91,8 @@
/// </summary>
/// <param name="action">The action to execute.</param>
/// <returns></returns>
public virtual Task OnUIThreadAsync(Func<Task> action) {
public virtual Task OnUIThreadAsync(Func<Task> action)
{
ValidateDispatcher();
#if WINDOWS_UWP
return dispatcher.RunTaskAsync(action);
Expand All @@ -100,27 +108,32 @@
/// </summary>
/// <param name="action">The action to execute.</param>
/// <exception cref="System.NotImplementedException"></exception>
public virtual void OnUIThread(System.Action action) {
public virtual void OnUIThread(System.Action action)
{
if (CheckAccess())
action();
else {
else
{
#if WINDOWS_UWP
dispatcher.RunAsync(CoreDispatcherPriority.Normal, () => action()).AsTask().Wait();
#else
Exception exception = null;
System.Action method = () => {
try {
System.Action method = () =>
{
try
{
action();
}
catch(Exception ex) {
catch (Exception ex)
{
exception = ex;
}

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
};

dispatcher.Invoke(method);

if (exception != null)
throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception);
throw new System.Reflection.TargetInvocationException("An error occurred while dispatching a call to the UI Thread", exception);
#endif
}
}
Expand All @@ -138,7 +151,8 @@
/// The WindowManager marks that element as a framework-created element so that it can determine what it created vs. what was intended by the developer.
/// Calling GetFirstNonGeneratedView allows the framework to discover what the original element was.
/// </remarks>
public virtual object GetFirstNonGeneratedView(object view) {
public virtual object GetFirstNonGeneratedView(object view)
{
return View.GetFirstNonGeneratedView(view);
}

Expand All @@ -158,9 +172,11 @@
/// </summary>
/// <param name="view">The view.</param>
/// <param name="handler">The handler.</param>
public virtual void ExecuteOnFirstLoad(object view, Action<object> handler) {
public virtual void ExecuteOnFirstLoad(object view, Action<object> handler)
{
var element = view as FrameworkElement;
if (element != null && !(bool) element.GetValue(PreviouslyAttachedProperty)) {
if (element != null && !(bool)element.GetValue(PreviouslyAttachedProperty))
{
element.SetValue(PreviouslyAttachedProperty, true);
View.ExecuteOnLoad(element, (s, e) => handler(s));
}
Expand All @@ -171,9 +187,11 @@
/// </summary>
/// <param name="view">The view.</param>
/// <param name="handler">The handler.</param>
public virtual void ExecuteOnLayoutUpdated(object view, Action<object> handler) {
public virtual void ExecuteOnLayoutUpdated(object view, Action<object> handler)
{
var element = view as FrameworkElement;
if (element != null) {
if (element != null)
{
View.ExecuteOnLayoutUpdated(element, (s, e) => handler(s));
}
}
Expand All @@ -190,7 +208,8 @@
/// <exception cref="System.NotImplementedException"></exception>
public virtual Func<CancellationToken, Task> GetViewCloseAction(object viewModel, ICollection<object> views, bool? dialogResult)
{
foreach (var contextualView in views) {
foreach (var contextualView in views)
{
var viewType = contextualView.GetType();
#if WINDOWS_UWP
var closeMethod = viewType.GetRuntimeMethod("Close", new Type[0]);
Expand All @@ -200,7 +219,8 @@
var closeMethod = viewType.GetMethod("Close", new Type[0]);
#endif
if (closeMethod != null)
return ct => {
return ct =>
{
#if AVALONIA
if (dialogResult != null)
{
Expand Down Expand Up @@ -234,7 +254,8 @@
#else
var isOpenProperty = viewType.GetProperty("IsOpen");
#endif
if (isOpenProperty != null) {
if (isOpenProperty != null)
{
return ct =>
{
isOpenProperty.SetValue(contextualView, false, null);
Expand Down
Loading