loplugin:redundantcast: reinterpret_cast to void*

Change-Id: I947b49cfb15f0e7d6ddfaae386656c70e4bd48ba
This commit is contained in:
Stephan Bergmann
2015-05-12 18:27:37 +02:00
parent 4e871a9b22
commit 0bfc98e63b
10 changed files with 59 additions and 46 deletions

View File

@@ -207,11 +207,10 @@ bool RedundantCast::VisitImplicitCastExpr(const ImplicitCastExpr * expr) {
bool RedundantCast::VisitCXXReinterpretCastExpr( bool RedundantCast::VisitCXXReinterpretCastExpr(
CXXReinterpretCastExpr const * expr) CXXReinterpretCastExpr const * expr)
{ {
if (ignoreLocation(expr) if (ignoreLocation(expr)) {
|| !expr->getSubExpr()->getType()->isVoidPointerType())
{
return true; return true;
} }
if (expr->getSubExpr()->getType()->isVoidPointerType()) {
auto t = expr->getType()->getAs<PointerType>(); auto t = expr->getType()->getAs<PointerType>();
if (t == nullptr || !t->getPointeeType()->isObjectType()) { if (t == nullptr || !t->getPointeeType()->isObjectType()) {
return true; return true;
@@ -219,13 +218,14 @@ bool RedundantCast::VisitCXXReinterpretCastExpr(
if (rewriter != nullptr) { if (rewriter != nullptr) {
auto loc = expr->getLocStart(); auto loc = expr->getLocStart();
while (compiler.getSourceManager().isMacroArgExpansion(loc)) { while (compiler.getSourceManager().isMacroArgExpansion(loc)) {
loc = compiler.getSourceManager().getImmediateMacroCallerLoc(loc); loc = compiler.getSourceManager().getImmediateMacroCallerLoc(
loc);
} }
if (compat::isMacroBodyExpansion(compiler, loc)) { if (compat::isMacroBodyExpansion(compiler, loc)) {
auto loc2 = expr->getLocEnd(); auto loc2 = expr->getLocEnd();
while (compiler.getSourceManager().isMacroArgExpansion(loc2)) { while (compiler.getSourceManager().isMacroArgExpansion(loc2)) {
loc2 = compiler.getSourceManager().getImmediateMacroCallerLoc( loc2 = compiler.getSourceManager()
loc2); .getImmediateMacroCallerLoc(loc2);
} }
if (compat::isMacroBodyExpansion(compiler, loc2)) { if (compat::isMacroBodyExpansion(compiler, loc2)) {
//TODO: check loc, loc2 are in same macro body expansion //TODO: check loc, loc2 are in same macro body expansion
@@ -236,7 +236,8 @@ bool RedundantCast::VisitCXXReinterpretCastExpr(
auto n = Lexer::MeasureTokenLength( auto n = Lexer::MeasureTokenLength(
loc, compiler.getSourceManager(), compiler.getLangOpts()); loc, compiler.getSourceManager(), compiler.getLangOpts());
std::string tok(s, n); std::string tok(s, n);
if (tok == "reinterpret_cast" && replaceText(loc, n, "static_cast")) { if (tok == "reinterpret_cast" && replaceText(loc, n, "static_cast"))
{
return true; return true;
} }
} }
@@ -246,6 +247,19 @@ bool RedundantCast::VisitCXXReinterpretCastExpr(
expr->getExprLoc()) expr->getExprLoc())
<< expr->getSubExprAsWritten()->getType() << expr->getType() << expr->getSubExprAsWritten()->getType() << expr->getType()
<< expr->getSourceRange(); << expr->getSourceRange();
} else if (expr->getType()->isVoidPointerType()) {
auto t = expr->getSubExpr()->getType()->getAs<PointerType>();
if (t == nullptr || !t->getPointeeType()->isObjectType()) {
return true;
}
report(
DiagnosticsEngine::Warning,
("reinterpret_cast from %0 to %1 can be simplified to static_cast"
" or an implicit conversion"),
expr->getExprLoc())
<< expr->getSubExprAsWritten()->getType() << expr->getType()
<< expr->getSourceRange();
}
return true; return true;
} }

View File

@@ -242,7 +242,7 @@ extern "C" { static void s_releaseAndRevoke_v(va_list * pParam)
uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *); uno_ExtEnvironment * pEnv = va_arg(*pParam, uno_ExtEnvironment *);
uno_Interface * pUnoI = va_arg(*pParam, uno_Interface *); uno_Interface * pUnoI = va_arg(*pParam, uno_Interface *);
pEnv->revokeInterface(pEnv, reinterpret_cast<void *>(pUnoI)); pEnv->revokeInterface(pEnv, pUnoI);
pUnoI->release(pUnoI); pUnoI->release(pUnoI);
}} }}

View File

@@ -176,7 +176,7 @@ void SAL_CALL OInterceptor::dispatch( const URL& _URL,const Sequence<PropertyVal
DispatchHelper* pHelper = new DispatchHelper; DispatchHelper* pHelper = new DispatchHelper;
pHelper->aArguments = Arguments; pHelper->aArguments = Arguments;
pHelper->aURL = _URL; pHelper->aURL = _URL;
Application::PostUserEvent( LINK( this, OInterceptor, OnDispatch ), reinterpret_cast< void* >( pHelper ) ); Application::PostUserEvent( LINK( this, OInterceptor, OnDispatch ), pHelper );
return; return;
} }
} }

View File

@@ -496,7 +496,7 @@ void OTasksWindow::fillTaskEntryList( const TaskEntryList& _rList )
for ( TaskEntryList::const_iterator pTask = _rList.begin(); pTask != aEnd; ++pTask, ++pImages ) for ( TaskEntryList::const_iterator pTask = _rList.begin(); pTask != aEnd; ++pTask, ++pImages )
{ {
SvTreeListEntry* pEntry = m_aCreation->InsertEntry( pTask->sTitle ); SvTreeListEntry* pEntry = m_aCreation->InsertEntry( pTask->sTitle );
pEntry->SetUserData( reinterpret_cast< void* >( new TaskEntry( *pTask ) ) ); pEntry->SetUserData( new TaskEntry( *pTask ) );
Image aImage = Image( *pImages ); Image aImage = Image( *pImages );
m_aCreation->SetExpandedEntryBmp( pEntry, aImage ); m_aCreation->SetExpandedEntryBmp( pEntry, aImage );

View File

@@ -927,7 +927,7 @@ OutlineToImpressFinalizer::OutlineToImpressFinalizer (
const ErrCode nErrorCode ( const ErrCode nErrorCode (
rBytes.ReadAt( rBytes.ReadAt(
nReadPosition, nReadPosition,
reinterpret_cast<void*>(pBuffer.get()), pBuffer.get(),
nBufferSize, nBufferSize,
&nReadByteCount)); &nReadByteCount));
@@ -951,7 +951,7 @@ OutlineToImpressFinalizer::OutlineToImpressFinalizer (
// Append the read bytes to the end of the memory stream. // Append the read bytes to the end of the memory stream.
if (nReadByteCount > 0) if (nReadByteCount > 0)
{ {
mpStream->Write(reinterpret_cast<void*>(pBuffer.get()), nReadByteCount); mpStream->Write(pBuffer.get(), nReadByteCount);
nReadPosition += nReadByteCount; nReadPosition += nReadByteCount;
} }
} }

View File

@@ -55,8 +55,7 @@ void PreviewValueSet::SetRightMouseClickHandler (const Link<>& rLink)
void PreviewValueSet::MouseButtonDown (const MouseEvent& rEvent) void PreviewValueSet::MouseButtonDown (const MouseEvent& rEvent)
{ {
if (rEvent.IsRight()) if (rEvent.IsRight())
maRightMouseClickHandler.Call(reinterpret_cast<void*>( maRightMouseClickHandler.Call(&const_cast<MouseEvent&>(rEvent));
&const_cast<MouseEvent&>(rEvent)));
else else
ValueSet::MouseButtonDown (rEvent); ValueSet::MouseButtonDown (rEvent);

View File

@@ -590,7 +590,7 @@ namespace sfx2 {
} }
#define NEW_ENTRY( url, bool ) \ #define NEW_ENTRY( url, bool ) \
reinterpret_cast<void*>( new IndexEntry_Impl( url, bool ) ) new IndexEntry_Impl( url, bool )
#define UNIFY_AND_INSERT_TOKEN( aToken ) \ #define UNIFY_AND_INSERT_TOKEN( aToken ) \
it = aInfo.insert( sfx2::KeywordInfo::value_type( aToken, 0 ) ).first; \ it = aInfo.insert( sfx2::KeywordInfo::value_type( aToken, 0 ) ).first; \

View File

@@ -108,7 +108,7 @@ void recently_used_file::truncate(off_t length)
size_t recently_used_file::read(char* buffer, size_t size) const size_t recently_used_file::read(char* buffer, size_t size) const
{ {
size_t r = fread(reinterpret_cast<void*>(buffer), sizeof(char), size, file_); size_t r = fread(buffer, sizeof(char), size, file_);
if ((r < size) && ferror(file_)) if ((r < size) && ferror(file_))
throw "I/O error: read failed"; throw "I/O error: read failed";
@@ -119,7 +119,7 @@ size_t recently_used_file::read(char* buffer, size_t size) const
void recently_used_file::write(const char* buffer, size_t size) const void recently_used_file::write(const char* buffer, size_t size) const
{ {
if (size != fwrite(reinterpret_cast<const void*>(buffer), sizeof(char), size, file_)) if (size != fwrite(buffer, sizeof(char), size, file_))
throw "I/O error: write failed"; throw "I/O error: write failed";
} }

View File

@@ -396,7 +396,7 @@ OString GraphicObject::GetUniqueID() const
SvStream* GraphicObject::GetSwapStream() const SvStream* GraphicObject::GetSwapStream() const
{ {
if( HasSwapStreamHdl() ) if( HasSwapStreamHdl() )
return reinterpret_cast<SvStream*>( mpSwapStreamHdl->Call( const_cast<void*>(reinterpret_cast<const void*>(this)) ) ); return reinterpret_cast<SvStream*>( mpSwapStreamHdl->Call( const_cast<void*>(static_cast<const void*>(this)) ) );
else else
return GRFMGR_AUTOSWAPSTREAM_NONE; return GRFMGR_AUTOSWAPSTREAM_NONE;
} }

View File

@@ -668,7 +668,7 @@ gboolean ensure_dbus_setup( gpointer data )
// Create menu model and action group attached to this frame. // Create menu model and action group attached to this frame.
GMenuModel* pMenuModel = G_MENU_MODEL( g_lo_menu_new() ); GMenuModel* pMenuModel = G_MENU_MODEL( g_lo_menu_new() );
GActionGroup* pActionGroup = reinterpret_cast<GActionGroup*>(g_lo_action_group_new( reinterpret_cast< gpointer >( pSalFrame ) )); GActionGroup* pActionGroup = reinterpret_cast<GActionGroup*>(g_lo_action_group_new( static_cast< gpointer >( pSalFrame ) ));
// Generate menu paths. // Generate menu paths.
::Window windowId = GDK_WINDOW_XID( gdkWindow ); ::Window windowId = GDK_WINDOW_XID( gdkWindow );