From c211c9fe8061abf65e87d9d4be93984019951b3b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Galais?= Date: Wed, 14 May 2025 01:17:52 +0200 Subject: [PATCH 1/3] RFC creation - 20250514-locals-unused-detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Galais --- rfc/20250514-locals-unused-detection.md | 53 +++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 rfc/20250514-locals-unused-detection.md diff --git a/rfc/20250514-locals-unused-detection.md b/rfc/20250514-locals-unused-detection.md new file mode 100644 index 0000000000..6dd33c50d0 --- /dev/null +++ b/rfc/20250514-locals-unused-detection.md @@ -0,0 +1,53 @@ +# Locals unused detection + +Issue: [2583](https://github.com/opentofu/opentofu/issues/2583) + +## Summary + +This proposal introduces warnings during validation when a local value (`local`) is declared in a module but never referenced. The aim is to improve readability, maintainability, and code quality by identifying unused definitions. + +Currently, OpenTofu does not emit any warning when a local value is declared but unused. This can lead to configuration bloat, making codebases harder to read and maintain. By warning on unused values, developers are encouraged to clean up stale or redundant code, enhancing the overall quality of OpenTofu configurations. + +## Proposed Solution + +- **Local Values**: + If a value declared inside a `locals` block is not referenced elsewhere in the module, a warning is emitted during validation. + +### User Documentation + +```bash +tofu validate + +│ Warning: The local "this_is_my_local" is unused. +│ +│ on local.tf line 9: +│ 9: this_is_my_local="local_value" + +│ Warning: The local "this_is_my_local" is unused. +│ +│ on mod1/mod2/local.tf line 9: +│ 9: this_is_my_local="local_value" + +Success! The configuration is valid. +``` + +### Technical Approach + +The validation process must perform static analysis of the configuration to determine whether each declared local value is actually used. This will require traversing the module's dependency graph and checking references. + +Regarding [20241118-module-vars-and-outputs-deprecation RFC](https://github.com/opentofu/opentofu/blob/main/rfc/20241118-module-vars-and-outputs-deprecation.md#silencing-deprecation-warnings-for-dependencies) we can use a dedicated flag "deprecation" to activate the detection feature to avoid weird interactions for users that don't really care about this. + +The "deprecation" Flag can be define the warning type that we want to have (module:all, module:local, module:none) according with the RFC. + +### Future Considerations + +- **Unused Provider Configurations**: + These are out of scope for this RFC. Providers are propagated automatically and require a separate design consideration.[2537](https://github.com/opentofu/opentofu/issues/2537) + +## Potential Alternatives + +- **Emit errors instead of warnings**: + This was deemed too aggressive and could introduce friction for existing users. + +- **Custom scripts**: + Currently we must to create custom script to detect unused locals. I think it's not a good solution to use custom script in addition of OpenTofu. From f93bc65515bbc505ae54b126ee4ae874744dd4c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Galais?= Date: Wed, 19 Nov 2025 20:11:49 +0100 Subject: [PATCH 2/3] RFC creation - 20250514-locals-unused-detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Galais --- rfc/20250514-locals-unused-detection.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rfc/20250514-locals-unused-detection.md b/rfc/20250514-locals-unused-detection.md index 6dd33c50d0..e0e2c57f35 100644 --- a/rfc/20250514-locals-unused-detection.md +++ b/rfc/20250514-locals-unused-detection.md @@ -37,7 +37,7 @@ The validation process must perform static analysis of the configuration to dete Regarding [20241118-module-vars-and-outputs-deprecation RFC](https://github.com/opentofu/opentofu/blob/main/rfc/20241118-module-vars-and-outputs-deprecation.md#silencing-deprecation-warnings-for-dependencies) we can use a dedicated flag "deprecation" to activate the detection feature to avoid weird interactions for users that don't really care about this. -The "deprecation" Flag can be define the warning type that we want to have (module:all, module:local, module:none) according with the RFC. +The "deprecation" flag can be define the warning type that we want to have (module:all, module:local, module:none) according with the RFC. ### Future Considerations From 0b058642e3c60a9a26243afce56dd6b5f8fa9994 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=B4me=20Galais?= Date: Wed, 19 Nov 2025 20:17:06 +0100 Subject: [PATCH 3/3] RFC creation - 20250514-locals-unused-detection MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Galais --- rfc/20250514-locals-unused-detection.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rfc/20250514-locals-unused-detection.md b/rfc/20250514-locals-unused-detection.md index e0e2c57f35..f9dacb0483 100644 --- a/rfc/20250514-locals-unused-detection.md +++ b/rfc/20250514-locals-unused-detection.md @@ -37,7 +37,7 @@ The validation process must perform static analysis of the configuration to dete Regarding [20241118-module-vars-and-outputs-deprecation RFC](https://github.com/opentofu/opentofu/blob/main/rfc/20241118-module-vars-and-outputs-deprecation.md#silencing-deprecation-warnings-for-dependencies) we can use a dedicated flag "deprecation" to activate the detection feature to avoid weird interactions for users that don't really care about this. -The "deprecation" flag can be define the warning type that we want to have (module:all, module:local, module:none) according with the RFC. +The "deprecation" flag can be define the warning scope that we want to have (module:all, module:local, module:none) according with the RFC. ### Future Considerations