Files
odfstudio/compilerplugins/clang/test/smartpointerderefaddress.cxx
T
Neil Roberts 48535514d4 Add a compiler plugin to detect &*smart_pointer
Adds a compiler plugin to detect cases of dereferencing a smart
pointer (such as unique_ptr or UnoCursorPointer) and then immediately
taking the address of the reference. In that case it is better to just
call get() on the pointer. This is particularly important if the pointer
can be null because in that case the initial dereference results in
undefined behaviour.

Change-Id: Ib1cca00c9180572f446721b90f83aa7fddb88c7d
Reviewed-on: https://gerrit.libreoffice.org/c/core/+/211756
Reviewed-by: Neil Roberts <nroberts@libreoffice.org>
Tested-by: Jenkins
2026-10-07 08:42:08 +02:00

43 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* This file is part of the LibreOffice project.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <sal/config.h>
#include <memory>
namespace sw
{
// Mockup of UnoCursorPointer because it’s probably difficult to include the header
class UnoCursorPointer
{
public:
int& operator*() { return *m_pInteger; };
private:
int* m_pInteger;
};
}
int* function1(std::unique_ptr<int>& x)
{
return &*x; // expected-error-re {{'*' followed by '&' operating on '{{.*}}unique_ptr{{.*}}', rather use '.get()' [loplugin:smartpointerderefaddress]}}
}
int* function2(std::shared_ptr<int>& x)
{
return &*x; // expected-error-re {{'*' followed by '&' operating on '{{.*}}shared_ptr{{.*}}', rather use '.get()' [loplugin:smartpointerderefaddress]}}
}
int* function3(sw::UnoCursorPointer& x)
{
return &*x; // expected-error-re {{'*' followed by '&' operating on '{{.*}}UnoCursorPointer{{.*}}', rather use '.get()' [loplugin:smartpointerderefaddress]}}
}
/* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */