diff --git a/rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll b/rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll index 5aeec330a37d..ed349df48681 100644 --- a/rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll +++ b/rust/ql/lib/codeql/rust/elements/internal/LocatableImpl.qll @@ -46,11 +46,25 @@ module Impl { predicate fromSource() { exists(this.getFile().getRelativePath()) } } + private @location_default getDbLocation(Locatable l) { + locatable_locations(Synth::convertLocatableToRaw(l), result) + } + + private MacroCall getImmediatelyEnclosingMacroCall(AstNode n) { + result = n.getParentNode() + or + exists(AstNode mid | + result = getImmediatelyEnclosingMacroCall(mid) and + n.getParentNode() = mid and + not mid instanceof MacroCall + ) + } + /** Gets the non-synthesized location of `l`, if any. */ LocationImpl::LocationDefault getLocationDefault(Locatable l) { - exists(@location_default location | - result = LocationImpl::TLocationDefault(location) and - locatable_locations(Synth::convertLocatableToRaw(l), location) - ) + result = LocationImpl::TLocationDefault(getDbLocation(l)) + or + not exists(getDbLocation(l)) and + result = getLocationDefault(getImmediatelyEnclosingMacroCall(l)) } } diff --git a/rust/ql/lib/codeql/rust/internal/AstConsistency.qll b/rust/ql/lib/codeql/rust/internal/AstConsistency.qll index b86ebc9a0f90..d812bfd2ef77 100644 --- a/rust/ql/lib/codeql/rust/internal/AstConsistency.qll +++ b/rust/ql/lib/codeql/rust/internal/AstConsistency.qll @@ -25,7 +25,8 @@ query predicate multipleLocations(Locatable e) { strictcount(e.getLocation()) > * Holds if `e` does not have a `Location`. */ query predicate noLocation(Locatable e) { - not exists(e.getLocation()) and not e.(AstNode).getParentNode*() = any(Crate c).getModule() + not exists(e.getLocation()) and + not e.(AstNode).getParentNode*() = any(Crate c).getModule() } private predicate multiplePrimaryQlClasses(Element e) { diff --git a/rust/ql/test/extractor-tests/generated/MacroItems/CONSISTENCY/AstConsistency.expected b/rust/ql/test/extractor-tests/generated/MacroItems/CONSISTENCY/AstConsistency.expected deleted file mode 100644 index 807f782967d6..000000000000 --- a/rust/ql/test/extractor-tests/generated/MacroItems/CONSISTENCY/AstConsistency.expected +++ /dev/null @@ -1,41 +0,0 @@ -noLocation -| file://:0:0:0:0 | ... .unwrap() | -| file://:0:0:0:0 | ...: ... | -| file://:0:0:0:0 | ...::Path | -| file://:0:0:0:0 | ...::Path | -| file://:0:0:0:0 | ...::path | -| file://:0:0:0:0 | ArgList | -| file://:0:0:0:0 | ArgList | -| file://:0:0:0:0 | ParamList | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | Path | -| file://:0:0:0:0 | RefTypeRepr | -| file://:0:0:0:0 | RefTypeRepr | -| file://:0:0:0:0 | RetTypeRepr | -| file://:0:0:0:0 | StmtList | -| file://:0:0:0:0 | fn get_parent | -| file://:0:0:0:0 | get_parent | -| file://:0:0:0:0 | parent | -| file://:0:0:0:0 | path | -| file://:0:0:0:0 | path | -| file://:0:0:0:0 | path | -| file://:0:0:0:0 | path | -| file://:0:0:0:0 | path | -| file://:0:0:0:0 | path | -| file://:0:0:0:0 | path | -| file://:0:0:0:0 | path | -| file://:0:0:0:0 | path.parent() | -| file://:0:0:0:0 | std | -| file://:0:0:0:0 | std | -| file://:0:0:0:0 | std | -| file://:0:0:0:0 | unwrap | -| file://:0:0:0:0 | use ...::Path | -| file://:0:0:0:0 | { ... } | diff --git a/rust/ql/test/extractor-tests/generated/MacroItems/MacroItems_getItem.expected b/rust/ql/test/extractor-tests/generated/MacroItems/MacroItems_getItem.expected index 08f3925f5a47..e86dfee101a5 100644 --- a/rust/ql/test/extractor-tests/generated/MacroItems/MacroItems_getItem.expected +++ b/rust/ql/test/extractor-tests/generated/MacroItems/MacroItems_getItem.expected @@ -1,2 +1,2 @@ -| gen_macro_items.rs:5:5:5:38 | MacroItems | 0 | file://:0:0:0:0 | use ...::Path | -| gen_macro_items.rs:5:5:5:38 | MacroItems | 1 | file://:0:0:0:0 | fn get_parent | +| gen_macro_items.rs:5:5:5:38 | MacroItems | 0 | gen_macro_items.rs:5:5:5:38 | use ...::Path | +| gen_macro_items.rs:5:5:5:38 | MacroItems | 1 | gen_macro_items.rs:5:5:5:38 | fn get_parent |