更新 : 2007 年 11 月
TypeName |
DoNotIgnoreMethodResults |
CheckId |
CA1806 |
カテゴリ |
Microsoft.Usage |
互換性に影響する変更点 |
なし |
原因
新しいオブジェクトが作成されましたが、使用されていません。
または
新しい文字列を作成して返すメソッドが呼び出されましたが、新しい文字列が使用されていません。
または
COM メソッドまたは P/Invoke メソッドから返された HRESULT またはエラー コードが使用されていません。
規則の説明
この規則により、コードで以下のような状況が発生している場合に、それを認識することができます。
不要なオブジェクトの作成や、使用されないオブジェクトに関連するガベージ コレクションは、パフォーマンスの低下を招きます。
文字列が変更不可になっています。ToUpper などのメソッドは、呼び出し元メソッドで文字列のインスタンスを変更するのではなく、文字列の新しいインスタンスを返します。
HRESULT またはエラー コードを無視すると、エラー状態で予期しない動作が発生したり、リソース不足状態になったりする可能性があります。
違反の修正方法
メソッド A でオブジェクト B の新しいインスタンスを作成し、そのインスタンスが使用されていない場合は、そのインスタンスを別のメソッドに引数として渡して、変数に代入します。オブジェクトの作成が必要なければ、そのコードを削除します。
または
メソッド A でメソッド B を呼び出し、メソッド B から返された新しい文字列インスタンスを使用していない場合は、そのインスタンスを別のメソッドに引数として渡して、変数に代入します。または、必要なければ呼び出しを削除します。
または
メソッド A でメソッド B を呼び出し、メソッド B から返された HRESULT またはエラー コードを使用していない場合は、その結果を条件付きステートメントで使用して、結果を変数に割り当てます。または、その結果を別のメソッドに引数として渡します。
警告を抑制する状況
オブジェクト作成の操作に何らかの目的がある場合を除いて、この規則による警告は抑制しないでください。
使用例
Trim を呼び出した結果を無視するクラスの例を次に示します。
Imports System
Namespace Samples
Public Class Book
Private ReadOnly _Title As String
Public Sub New(ByVal title As String)
If title IsNot Nothing Then
' Violates this rule
title.Trim()
End If
_Title = title
End Sub
Public ReadOnly Property Title() As String
Get
Return _Title
End Get
End Property
End Class
End Namespace
using System;
namespace Samples
{
public class Book
{
private readonly string _Title;
public Book(string title)
{
if (title != null)
{
// Violates this rule
title.Trim();
}
_Title = title;
}
public string Title
{
get { return _Title; }
}
}
}
using namespace System;
namespace Samples
{
public ref class Book
{
private:
initonly String^ _Title;
public:
Book(String^ title)
{
if (title != nullptr)
{
// Violates this rule
title->Trim();
}
_Title = title;
}
property String^ Title
{
String^ get() { return _Title; }
}
};
}
Trim の結果を呼び出し元の変数に代入することによって上記の違反を修正するコード例を次に示します。
Imports System
Namespace Samples
Public Class Book
Private ReadOnly _Title As String
Public Sub New(ByVal title As String)
If title IsNot Nothing Then
title = title.Trim()
End If
_Title = title
End Sub
Public ReadOnly Property Title() As String
Get
Return _Title
End Get
End Property
End Class
End Namespace
using System;
namespace Samples
{
public class Book
{
private readonly string _Title;
public Book(string title)
{
if (title != null)
{
title = title.Trim();
}
_Title = title;
}
public string Title
{
get { return _Title; }
}
}
}
using namespace System;
namespace Samples
{
public ref class Book
{
private:
initonly String^ _Title;
public:
Book(String^ title)
{
if (title != nullptr)
{
title = title->Trim();
}
_Title = title;
}
property String^ Title
{
String^ get() { return _Title; }
}
};
}
作成したオブジェクトを使用しないメソッドの例を次に示します。
![]() |
---|
この違反は、Visual Basic では再現できません。 |
using System;
namespace Samples
{
public class Book
{
public Book()
{
}
public static Book CreateBook()
{
// Violates this rule
new Book();
return new Book();
}
}
}
using namespace System;
namespace Samples
{
public ref class Book
{
public:
Book()
{
}
static Book^ CreateBook()
{
// Violates this rule
gcnew Book();
return gcnew Book();
}
};
}
オブジェクトの不要な作成を削除することによって上記の違反を修正するコード例を次に示します。
using System;
namespace Samples
{
public class Book
{
public Book()
{
}
public static Book CreateBook()
{
return new Book();
}
}
}
using namespace System;
namespace Samples
{
public ref class Book
{
public:
Book()
{
}
static Book^ CreateBook()
{
return gcnew Book();
}
};
}
GetShortPathName というネイティブ メソッドから返されたエラー コードを無視するメソッドの例を次に示します。
Imports System
Imports System.ComponentModel
Imports System.IO
Imports System.Runtime.InteropServices
Imports System.Text
Namespace Samples
Public Module FileIO
Public Function GetShortPath(ByVal longPath As String) As String
longPath = Path.GetFullPath(longPath)
Const MAX_PATH As Integer = 260
Dim shortPathBuffer As New StringBuilder(MAX_PATH)
' Violates this rule
NativeMethods.GetShortPathName(longPath, shortPathBuffer, shortPathBuffer.Capacity)
Return shortPathBuffer.ToString()
End Function
End Module
Friend Module NativeMethods
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Public Function GetShortPathName(ByVal lpszLongPath As String, ByVal lpszShortPath As StringBuilder, ByVal cchBuffer As UInteger) As UInteger
End Function
End Module
End Namespace
using System;
using System.ComponentModel;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Samples
{
public static class FileIO
{
public static string GetShortPath(string longPath)
{
longPath = Path.GetFullPath(longPath);
const int MAX_PATH = 260;
StringBuilder shortPathBuffer = new StringBuilder(MAX_PATH);
// Violates this rule
NativeMethods.GetShortPathName(longPath, shortPathBuffer, (uint)shortPathBuffer.Capacity);
return shortPathBuffer.ToString();
}
}
internal static class NativeMethods
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer);
}
}
#include "stdafx.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::IO;
using namespace System::Runtime::InteropServices;
using namespace System::Text;
namespace Samples
{
private ref class NativeMethods sealed
{
private:
NativeMethods()
{
}
internal:
[DllImport("kernel32.dll", CharSet=CharSet::Auto, SetLastError=true, BestFitMapping=false, ThrowOnUnmappableChar=true)]
static unsigned int GetShortPathName(String^ lpszLongPath, StringBuilder^ lpszShortPath, unsigned int cchBuffer);
};
public ref class FileIO sealed
{
private:
FileIO()
{
}
public:
static String^ GetShortPath(String^ longPath)
{
longPath = Path::GetFullPath(longPath);
const int MAX_PATH = 260;
StringBuilder^ shortPathBuffer = gcnew StringBuilder(MAX_PATH);
// Violates this rule
NativeMethods::GetShortPathName(longPath, shortPathBuffer, shortPathBuffer->Capacity);
return shortPathBuffer->ToString();
}
};
}
呼び出しに失敗したときに、エラー コードをチェックして例外をスローすることによって上記の違反を修正するコード例を次に示します。
Namespace Samples
Public Module FileIO_1
Public Function GetShortPath(ByVal longPath As String) As String
longPath = Path.GetFullPath(longPath)
Const MAX_PATH As Integer = 260
Dim shortPathBuffer As New StringBuilder(MAX_PATH)
' GetShortPathName returns 0 when the operation fails
If NativeMethods.GetShortPathName(longPath, shortPathBuffer, shortPathBuffer.Capacity) = 0 Then
' Note: The constructor of Win32Exception will automatically
' set Win32Exception.NativeErrorCode to Marshal.GetLastWin32Error()
Throw New Win32Exception()
End If
Return shortPathBuffer.ToString()
End Function
End Module
Friend Module NativeMethods_1
<DllImport("kernel32.dll", CharSet:=CharSet.Auto, SetLastError:=True, BestFitMapping:=False, ThrowOnUnmappableChar:=True)> _
Public Function GetShortPathName(ByVal lpszLongPath As String, ByVal lpszShortPath As StringBuilder, ByVal cchBuffer As UInteger) As UInteger
End Function
End Module
End Namespace
namespace Samples
{
public static class FileIO_1
{
public static string GetShortPath(string longPath)
{
const int MAX_PATH = 260;
StringBuilder shortPathBuffer = new StringBuilder(MAX_PATH);
// GetShortPathName returns 0 when the operation fails
if (NativeMethods.GetShortPathName(longPath, shortPathBuffer, (uint)shortPathBuffer.Capacity) == 0)
{
// Note: The constructor of Win32Exception will automatically
// set Win32Exception.NativeErrorCode to Marshal.GetLastWin32Error()
throw new Win32Exception();
}
return shortPathBuffer.ToString();
}
}
internal static class NativeMethods_1
{
[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true, BestFitMapping = false, ThrowOnUnmappableChar = true)]
public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer);
}
}
#include "stdafx.h"
using namespace System;
using namespace System::ComponentModel;
using namespace System::IO;
using namespace System::Runtime::InteropServices;
using namespace System::Text;
namespace Samples
{
private ref class NativeMethods_1 sealed
{
private:
void NativeMethods()
{
}
internal:
[DllImport("kernel32.dll", CharSet=CharSet::Auto, SetLastError=true, BestFitMapping=false, ThrowOnUnmappableChar=true)]
static unsigned int GetShortPathName(String^ lpszLongPath, StringBuilder^ lpszShortPath, unsigned int cchBuffer);
};
public ref class FileIO_1 sealed
{
private:
void FileIO()
{
}
public:
static String^ GetShortPath(String^ longPath)
{
longPath = Path::GetFullPath(longPath);
const int MAX_PATH = 260;
StringBuilder^ shortPathBuffer = gcnew StringBuilder(MAX_PATH);
// GetShortPathName returns 0 when the operation fails
if (NativeMethods::GetShortPathName(longPath, shortPathBuffer, shortPathBuffer->Capacity) == 0)
{
// Note: The constructor of Win32Exception will automatically
// set Win32Exception.NativeErrorCode to Marshal.GetLastWin32Error()
throw gcnew Win32Exception();
}
return shortPathBuffer->ToString();
}
};
}