]> git.bitcoin.ninja Git - rust-lightning/commitdiff
Use native check-cfg lint in cargo beta
authorDuncan Dean <git@dunxen.dev>
Fri, 12 Jul 2024 09:23:50 +0000 (11:23 +0200)
committerDuncan Dean <git@dunxen.dev>
Fri, 12 Jul 2024 09:48:15 +0000 (11:48 +0200)
This uses the newly introduced conditional configuration checks that are
now configurable withint Cargo (beta).

This allows us to get rid of our custom python script that checks for
expected features and cfgs.

This does introduce a warning regarding the unknown lint in Cargo
versions prior to the current beta, but since these are not rustc errors,
they won't break any builds with the "-D warnings" RUSTFLAG.

Moving to this lint actually exposed the "strict" feature not being
present in the lightning-invoice crate, as our python script didnt
correctly parse the cfg_attr where it appeared.

13 files changed:
.github/workflows/build.yml
Cargo.toml
ci/check-cfg-flags.py [deleted file]
lightning-background-processor/Cargo.toml
lightning-block-sync/Cargo.toml
lightning-custom-message/Cargo.toml
lightning-invoice/Cargo.toml
lightning-net-tokio/Cargo.toml
lightning-persister/Cargo.toml
lightning-rapid-gossip-sync/Cargo.toml
lightning-transaction-sync/Cargo.toml
lightning/Cargo.toml
possiblyrandom/Cargo.toml

index a7c6bcc21398afdcc1fd17157b9b92424ec6d6d2..e84cfa174f354da3a76a4c3ddfaae2848b13ca17 100644 (file)
@@ -32,8 +32,6 @@ jobs:
         run: |
           rustup target add thumbv7m-none-eabi
           sudo apt-get -y install gcc-arm-none-eabi
-      - name: Check for unknown cfg tags
-        run: ci/check-cfg-flags.py
       - name: shellcheck the CI script
         if: "matrix.platform == 'ubuntu-latest'"
         run: |
index ec9edb3ac33076af7833898f9c5347db4f34fda5..0aa7f7624d8e8ec9d739431adde26d8a4553ec29 100644 (file)
@@ -42,3 +42,25 @@ panic = "abort"
 
 [patch.crates-io.possiblyrandom]
 path = "possiblyrandom"
+
+[workspace.lints.rust.unexpected_cfgs]
+level = "forbid"
+# When adding a new cfg attribute, ensure that it is added to this list.
+#
+# Note that Cargo automatically declares corresponding cfgs for every feature
+# defined in the member-level [features] tables as "expected".
+check-cfg = [
+    "cfg(fuzzing)",
+    "cfg(secp256k1_fuzz)",
+    "cfg(hashes_fuzz)",
+    "cfg(test)",
+    "cfg(debug_assertions)",
+    "cfg(c_bindings)",
+    "cfg(ldk_bench)",
+    "cfg(taproot)",
+    "cfg(async_signing)",
+    "cfg(require_route_graph_test)",
+    "cfg(dual_funding)",
+    "cfg(splicing)",
+    "cfg(async_payments)",
+]
diff --git a/ci/check-cfg-flags.py b/ci/check-cfg-flags.py
deleted file mode 100755 (executable)
index d73bf50..0000000
+++ /dev/null
@@ -1,170 +0,0 @@
-#!/usr/bin/env python3
-# Rust is fairly relaxed in checking the validity of arguments passed to #[cfg].
-# While it should probably be more strict when checking features, it cannot be
-# strict when checking loose cfg tags, because those can be anything and are
-# simply passed to rustc via unconstrained arguments.
-#
-# Thus, we do it for rustc manually, but scanning all our source and checking
-# that all our cfg tags match a known cfg tag.
-import sys, glob, re
-
-def check_feature(feature):
-    if feature == "std":
-        pass
-    elif feature == "no-std":
-        pass
-    elif feature == "possiblyrandom":
-        pass
-    elif feature == "getrandom":
-        pass
-    elif feature == "hashbrown":
-        pass
-    elif feature == "backtrace":
-        pass
-    elif feature == "grind_signatures":
-        pass
-    elif feature == "unsafe_revoked_tx_signing":
-        pass
-    elif feature == "futures":
-        pass
-    elif feature == "tokio":
-        pass
-    elif feature == "rest-client":
-        pass
-    elif feature == "rpc-client":
-        pass
-    elif feature == "serde":
-        pass
-    elif feature == "esplora-blocking":
-        pass
-    elif feature == "esplora-async":
-        pass
-    elif feature == "async-interface":
-        pass
-    elif feature == "electrum":
-        pass
-    elif feature == "time":
-        pass
-    elif feature == "_test_utils":
-        pass
-    elif feature == "_test_vectors":
-        pass
-    elif feature == "afl":
-        pass
-    elif feature == "honggfuzz":
-        pass
-    elif feature == "libfuzzer_fuzz":
-        pass
-    elif feature == "stdin_fuzz":
-        pass
-    elif feature == "max_level_off":
-        pass
-    elif feature == "max_level_error":
-        pass
-    elif feature == "max_level_warn":
-        pass
-    elif feature == "max_level_info":
-        pass
-    elif feature == "max_level_debug":
-        pass
-    elif feature == "max_level_trace":
-        pass
-    else:
-        print("Bad feature: " + feature)
-        assert False
-
-def check_target_os(os):
-    if os == "windows":
-        pass
-    else:
-        assert False
-
-def check_cfg_tag(cfg):
-    if cfg == "fuzzing":
-        pass
-    elif cfg == "secp256k1_fuzz":
-        pass
-    elif cfg == "hashes_fuzz":
-        pass
-    elif cfg == "test":
-        pass
-    elif cfg == "debug_assertions":
-        pass
-    elif cfg == "c_bindings":
-        pass
-    elif cfg == "ldk_bench":
-        pass
-    elif cfg == "taproot":
-        pass
-    elif cfg == "async_signing":
-        pass
-    elif cfg == "require_route_graph_test":
-        pass
-    elif cfg == "dual_funding":
-        pass
-    elif cfg == "splicing":
-        pass
-    elif cfg == "async_payments":
-        pass
-    else:
-        print("Bad cfg tag: " + cfg)
-        assert False
-
-def check_cfg_args(cfg):
-    if cfg.startswith("all(") or cfg.startswith("any(") or cfg.startswith("not("):
-        brackets = 1
-        pos = 4
-        while pos < len(cfg):
-            if cfg[pos] == "(":
-                brackets += 1
-            elif cfg[pos] == ")":
-                brackets -= 1
-                if brackets == 0:
-                    check_cfg_args(cfg[4:pos])
-                    if pos + 1 != len(cfg):
-                        assert cfg[pos + 1] == ","
-                        check_cfg_args(cfg[pos + 2:].strip())
-                    return
-            pos += 1
-        assert False
-        assert(cfg.endswith(")"))
-        check_cfg_args(cfg[4:len(cfg)-1])
-    else:
-        parts = [part.strip() for part in cfg.split(",", 1)]
-        if len(parts) > 1:
-            for part in parts:
-                check_cfg_args(part)
-        elif cfg.startswith("feature") or cfg.startswith("target_os") or cfg.startswith("target_pointer_width"):
-            arg = cfg
-            if cfg.startswith("feature"):
-                arg = arg[7:].strip()
-            elif cfg.startswith("target_os"):
-                arg = arg[9:].strip()
-            else:
-                arg = arg[20:].strip()
-            assert arg.startswith("=")
-            arg = arg[1:].strip()
-            assert arg.startswith("\"")
-            assert arg.endswith("\"")
-            arg = arg[1:len(arg)-1]
-            assert not "\"" in arg
-            if cfg.startswith("feature"):
-                check_feature(arg)
-            elif cfg.startswith("target_os"):
-                check_target_os(arg)
-            else:
-                assert arg == "32" or arg == "64"
-        else:
-            check_cfg_tag(cfg.strip())
-
-cfg_regex = re.compile("#\[cfg\((.*)\)\]")
-for path in glob.glob(sys.path[0] + "/../**/*.rs", recursive = True):
-    with open(path, "r") as file:
-        while True:
-            line = file.readline()
-            if not line:
-                break
-            if "#[cfg(" in line:
-                if not line.strip().startswith("//"):
-                    cfg_part = cfg_regex.match(line.strip()).group(1)
-                    check_cfg_args(cfg_part)
index e563e2acd9b09a338a6e3ce52e2e1d682726d428..df4b2c8278e718a56e8f430e4000b0aef4bbbe30 100644 (file)
@@ -30,3 +30,6 @@ tokio = { version = "1.35", features = [ "macros", "rt", "rt-multi-thread", "syn
 lightning = { version = "0.0.123-beta", path = "../lightning", features = ["_test_utils"] }
 lightning-invoice = { version = "0.31.0-beta", path = "../lightning-invoice" }
 lightning-persister = { version = "0.0.123-beta", path = "../lightning-persister" }
+
+[lints]
+workspace = true
index b232a6ae4abbfd7bd740893ae3dcdbc021634e3e..2e827000017a63844ae3a2320f07e6c66bdfc03d 100644 (file)
@@ -28,3 +28,6 @@ chunked_transfer = { version = "1.4", optional = true }
 [dev-dependencies]
 lightning = { version = "0.0.123-beta", path = "../lightning", features = ["_test_utils"] }
 tokio = { version = "1.35", features = [ "macros", "rt" ] }
+
+[lints]
+workspace = true
index ce358159596172f69c3ce06a3ce5035395989de1..f74ead81c9702bab28e4a0b1a669a25ebd952ad0 100644 (file)
@@ -16,3 +16,6 @@ rustdoc-args = ["--cfg", "docsrs"]
 [dependencies]
 bitcoin = "0.31.2"
 lightning = { version = "0.0.123-beta", path = "../lightning" }
+
+[lints]
+workspace = true
index 22f8616a2f9731ab5be7c3c110870fe1e96cabb6..cd449d8d1a7371fdd37af4740fb9473f22bbae31 100644 (file)
@@ -18,6 +18,7 @@ rustdoc-args = ["--cfg", "docsrs"]
 default = ["std"]
 no-std = ["lightning/no-std"]
 std = ["bitcoin/std", "lightning/std", "bech32/std"]
+strict = []
 
 [dependencies]
 bech32 = { version = "0.9.1", default-features = false }
@@ -31,3 +32,6 @@ lightning = { version = "0.0.123-beta", path = "../lightning", default-features
 hex = { package = "hex-conservative", version = "0.1.1", default-features = false }
 serde_json = { version = "1"}
 hashbrown = { version = "0.13", default-features = false }
+
+[lints]
+workspace = true
index c340fc8564054d168b41b769abeefb94eec2d96d..c56553711454b4f3aae3885651f0b3793acd9b25 100644 (file)
@@ -22,3 +22,6 @@ tokio = { version = "1.35", features = [ "rt", "sync", "net", "time" ] }
 [dev-dependencies]
 tokio = { version = "1.35", features = [ "macros", "rt", "rt-multi-thread", "sync", "net", "time" ] }
 lightning = { version = "0.0.123-beta", path = "../lightning", features = ["_test_utils"] }
+
+[lints]
+workspace = true
index 49f63614e83bf4242b67b6fd19b84b322829b021..5f1e2774064f670c624b8d906079701c30f811af 100644 (file)
@@ -26,3 +26,6 @@ criterion = { version = "0.4", optional = true, default-features = false }
 [dev-dependencies]
 lightning = { version = "0.0.123-beta", path = "../lightning", features = ["_test_utils"] }
 bitcoin = { version = "0.31.2", default-features = false }
+
+[lints]
+workspace = true
index ad25e2f30849de487bf3ad6d8cfce27f68256bb5..7ca67a38ec8d5faf2507fda441d4917632023a08 100644 (file)
@@ -23,3 +23,6 @@ criterion = { version = "0.4", optional = true, default-features = false }
 
 [dev-dependencies]
 lightning = { version = "0.0.123-beta", path = "../lightning", features = ["_test_utils"] }
+
+[lints]
+workspace = true
index 23ead252a81ae5b1f8b58b90aed4c43a1a925674..ab796749954dbf7c33bf1e35e94ea800cf13b669 100644 (file)
@@ -39,3 +39,6 @@ electrsd = { version = "0.27.3", default-features = false, features = ["legacy",
 
 [target.'cfg(all(not(target_os = "windows"), no_download))'.dev-dependencies]
 electrsd = { version = "0.27.3", default-features = false, features = ["legacy"] }
+
+[lints]
+workspace = true
index 8f238f5b5573f6e9269b6950df784255cfc4cc7a..cf4a5329476dd2c88e9c5de9cf9fceb7d71ac740 100644 (file)
@@ -65,3 +65,6 @@ criterion = { version = "0.4", optional = true, default-features = false }
 
 [target.'cfg(taproot)'.dependencies]
 musig2 = { git = "https://github.com/arik-so/rust-musig2", rev = "739533fc" }
+
+[lints]
+workspace = true
index 5d8a35ccdf58e408454cfc0be4354a260618859b..4508f690129b574bdddc9f4ce76c3f3ebe57570f 100644 (file)
@@ -19,3 +19,6 @@ getrandom = { version = "0.2", optional = true, default-features = false }
 # Enable getrandom if we are on a platform that (likely) supports it
 [target.'cfg(not(any(target_os = "unknown", target_os = "none")))'.dependencies]
 getrandom = { version = "0.2", default-features = false }
+
+[lints]
+workspace = true