tst_QTemporaryFile: replace custom struct with QScopeGuard

Simplify the code by using QScopeGuard. In particular, this makes
clear what "reset()" was supposed to do: dismiss.

Every Qt developer knows the semantics of QScopeGuard, but this custom
class has to be learned by every reader anew.

As a drive-by, fix the spacing inside parentheses.

Amends the start of the public history.

Pick-to: 6.10 6.9 6.8 6.5
Change-Id: I20b80fe551951a5aba01a8996c4934b574d34fac
Reviewed-by: Mårten Nordheim <marten.nordheim@qt.io>
This commit is contained in:
Marc Mutz 2025-07-14 15:19:56 +02:00
parent 8c626ad667
commit 7943730389
1 changed files with 9 additions and 20 deletions

View File

@ -826,37 +826,26 @@ void tst_QTemporaryFile::autoRemoveAfterFailedRename()
#if defined(Q_OS_VXWORKS) #if defined(Q_OS_VXWORKS)
QSKIP("QTBUG-130066"); QSKIP("QTBUG-130066");
#endif #endif
struct CleanOnReturn
{ QString tempName;
~CleanOnReturn() auto cleaner = qScopeGuard([&] {
{
if (!tempName.isEmpty()) if (!tempName.isEmpty())
QFile::remove(tempName); QFile::remove(tempName);
} });
void reset()
{
tempName.clear();
}
QString tempName;
};
CleanOnReturn cleaner;
{ {
QTemporaryFile file; QTemporaryFile file;
QVERIFY( file.open() ); QVERIFY( file.open() );
cleaner.tempName = file.fileName(); tempName = file.fileName();
QVERIFY( QFile::exists(cleaner.tempName) ); QVERIFY(QFile::exists(tempName));
QVERIFY( !QFileInfo("i-do-not-exist").isDir() ); QVERIFY( !QFileInfo("i-do-not-exist").isDir() );
QVERIFY( !file.rename("i-do-not-exist/file.txt") ); QVERIFY( !file.rename("i-do-not-exist/file.txt") );
QVERIFY( QFile::exists(cleaner.tempName) ); QVERIFY(QFile::exists(tempName));
} }
QVERIFY( !QFile::exists(cleaner.tempName) ); QVERIFY(!QFile::exists(tempName));
cleaner.reset(); cleaner.dismiss(); // would fail: file is known to no longer exist
} }
void tst_QTemporaryFile::createNativeFile_data() void tst_QTemporaryFile::createNativeFile_data()