SuperfluousCasts

Removing Superfluous Casts

Adding superfluous casts in cases where the compiler can do the cast implicitly is bad as it:

  • Adds unneeded code and thus reducing from the precious line length available.
  • Can mask (prevent compiler warnings) incompatible type changes of the variables and functions involved.

Void Pointer Casts

The most common superfluous casts are from void pointers ("void *" and the Win32 variants "VOID *", "PVOID" and LPVOID) returned by functions to other pointer types. E.g.

  • s = (WCHAR *)HeapAlloc(GetProcessHeap(), 0, size);

To find those type of casts use the type-convs.pl script; an adapted version of the original found on http://kernelnewbies.org/KernelJanitors/Todo/VoidPointerConvs.

  • ./type-convs.pl wine.git/

will check the Wine source directory against the embedded list of void pointer returning functions. To ignore the built in function list and thus limit the output to a set of functions pass the function names as additional parameters to the script:

  • ./type-convs.pl wine.git/ HeapAlloc HeapReAlloc

The script will output a diff but won't change the source code itself. The generated diff is NOT suitable to be send to wine-patches; it needs to be cleaned up and tested before that.

find_voidp_funcs.pl can be used to generate the list of functions that return void pointers:

  • ./find_voidp_funcs.pl wine.git/

That was used to generate the embedded list in the type-convs.pl script.

Casting NULL

Casting NULL is almost always a pointless exercise. There are two distinct types of casts here:

Casting to a pointer type

This includes handles that are of pointer type e.g.

  • (WCHAR *)NULL, (HWND)NULL

Solution: Remove the cast.

Casting to an integer type

This includes handles that are of integer type e.g.

  • (HCRYPTKEY)NULL, (LRESULT)(HTREEITEM)NULL

Solution: Replace the NULL including the cast(s) with 0.


CategoryJanitorialProjects

SuperfluousCasts (last edited 2008-10-24 22:50:16 by MichaelStefaniuc)