Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) The Bitcoin Core developers2// Distributed under the MIT software license, see the accompanying3// file COPYING or https://opensource.org/license/mit/.5use std::process::Command;7use crate::util::{check_output, get_pathspecs_default_excludes, git, LintResult};9pub fn lint_includes_build_config() -> LintResult {10let config_path = "./cmake/bitcoin-build-config.h.in";11let defines_regex = format!(12r"^\s*(?!//).*({})",13check_output(Command::new("grep").args(["define", "--", config_path]))14.expect("grep failed")15.lines()16.map(|line| {17line.split_whitespace()18.nth(1)19.unwrap_or_else(|| panic!("Could not extract name in line: {line}"))20})21.collect::<Vec<_>>()22.join("|")23);24let print_affected_files = |mode: bool| {25// * mode==true: Print files which use the define, but lack the include26// * mode==false: Print files which lack the define, but use the include27let defines_files = check_output(28git()29.args([30"grep",31"--perl-regexp",32if mode {33"--files-with-matches"34} else {35"--files-without-match"36},37&defines_regex,38"--",39"*.cpp",40"*.h",41])42.args(get_pathspecs_default_excludes()),43)44.expect("grep failed");45git()46.args([47"grep",48if mode {49"--files-without-match"50} else {51"--files-with-matches"52},53if mode {54"^#include <bitcoin-build-config.h> // IWYU pragma: keep$"55} else {56"#include <bitcoin-build-config.h>" // Catch redundant includes with and without the IWYU pragma57},58"--",59])60.args(defines_files.lines())61.status()62.expect("command error")63.success()64};65let missing = print_affected_files(true);66if missing {67return Err(format!(68r#"69One or more files use a symbol declared in the bitcoin-build-config.h header. However, they are not70including the header. This is problematic, because the header may or may not be indirectly71included. If the indirect include were to be intentionally or accidentally removed, the build could72still succeed, but silently be buggy. For example, a slower fallback algorithm could be picked,73even though bitcoin-build-config.h indicates that a faster feature is available and should be used.75If you are unsure which symbol is used, you can find it with this command:76git grep --perl-regexp '{defines_regex}' -- file_name78Make sure to include it with the IWYU pragma. Otherwise, IWYU may falsely instruct to remove the79include again.81#include <bitcoin-build-config.h> // IWYU pragma: keep82"#83)84.trim()85.to_string());86}87let redundant = print_affected_files(false);88if redundant {89return Err(r#"90None of the files use a symbol declared in the bitcoin-build-config.h header. However, they are including91the header. Consider removing the unused include.92"#93.to_string());94}95Ok(())96}98pub fn lint_std_filesystem() -> LintResult {99let found = git()100.args([101"grep",102"--line-number",103"std::filesystem",104"--",105"./src/",106":(exclude)src/ipc/libmultiprocess/",107":(exclude)src/util/fs.h",108":(exclude)src/bitcoin-chainstate.cpp",109])110.status()111.expect("command error")112.success();113if found {114Err(r#"115Direct use of std::filesystem may be dangerous and buggy. Please include <util/fs.h> and use the116fs:: namespace, which has unsafe filesystem functions marked as deleted.117"#118.trim()119.to_string())120} else {121Ok(())122}123}125pub fn lint_remove_all() -> LintResult {126let found = git()127.args([128"grep",129"--line-number",130"remove_all(.*)",131"--",132"./src/",133// These are likely not avoidable.134":(exclude)src/test/kernel/test_kernel.cpp",135":(exclude)src/test/util/setup_common.cpp",136])137.status()138.expect("command error")139.success();140if found {141Err(r#"142Use of fs::remove_all or std::filesystem::remove_all is dangerous and should be avoided. If removal143is required, prefer fs::remove.144"#145.trim()146.to_string())147} else {148Ok(())149}150}152pub fn lint_rpc_assert() -> LintResult {153let found = git()154.args([155"grep",156"--line-number",157"--extended-regexp",158r"\<(A|a)ss(ume|ert)\(",159"--",160"src/rpc/",161"src/wallet/rpc*",162":(exclude)src/rpc/server.cpp",163// src/rpc/server.cpp is excluded from this check since it's mostly meta-code.164])165.status()166.expect("command error")167.success();168if found {169Err(r#"170CHECK_NONFATAL(condition) or NONFATAL_UNREACHABLE should be used instead of assert for RPC code.172Aborting the whole process is undesirable for RPC code. So nonfatal173checks should be used over assert. See: src/util/check.h174"#175.trim()176.to_string())177} else {178Ok(())179}180}182pub fn lint_boost_assert() -> LintResult {183let found = git()184.args([185"grep",186"--line-number",187"--extended-regexp",188r"BOOST_ASSERT\(",189"--",190"*.cpp",191"*.h",192])193.status()194.expect("command error")195.success();196if found {197Err(r#"198BOOST_ASSERT must be replaced with Assert, BOOST_REQUIRE, or BOOST_CHECK to avoid an unnecessary199include of the boost/assert.hpp dependency.200"#201.trim()202.to_string())203} else {204Ok(())205}206}
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.