PageSourceSearch

https://www.openpolicyagent.org/assets/js/bdcb869e.61037681.js

js openpolicyagent.org collected 2026-10-01 08:41:43 UTC 6,018 bytes, 1 lines download raw bytes

1"use strict";(self.webpackChunkopa_website=self.webpackChunkopa_website||[]).push([[15986],{28453:(e,r,s)=>{s.d(r,{R:()=>a,x:()=>t});var n=s(96540);const i={},o=n.createContext(i);function a(e){const r=n.useContext(o);return n.useMemo((function(){return"function"==typeof e?e(r):{...r,...e}}),[r,e])}function t(e){let r;return r=e.disableParentContext?"function"==typeof e.components?e.components(i):e.components||i:a(e.components),n.createElement(o.Provider,{value:r},e.children)}},95140:(e,r,s)=>{s.r(r),s.d(r,{assets:()=>d,contentTitle:()=>t,default:()=>h,frontMatter:()=>a,metadata:()=>n,toc:()=>l});const n=JSON.parse('{"id":"errors/rego-compile-error/assigned-var-name-unused","title":"rego_compile_error: assigned var {name} unused","description":"This error is caused by a variable being assigned a value, but never used. This is often the result of a typo or","source":"@site/docs/errors/rego-compile-error/assigned-var-name-unused.md","sourceDirName":"errors/rego-compile-error","slug":"/errors/rego-compile-error/assigned-var-name-unused","permalink":"/docs/errors/rego-compile-error/assigned-var-name-unused","draft":false,"unlisted":false,"tags":[],"version":"current","frontMatter":{"sidebar_label":"assigned var {name} unused","image":"/img/opa-errors.png"},"sidebar":"docsSidebar","previous":{"title":"rule {name} produced conflicting values","permalink":"/docs/errors/eval-conflict-error/rule-name-produced-conflicting-values"},"next":{"title":"unexpected assign token","permalink":"/docs/errors/rego-parse-error/unexpected-assign-token"}}');var i=s(74848),o=s(28453);const a={sidebar_label:"assigned var {name} unused",image:"/img/opa-errors.png"},t="rego_compile_error: assigned var {name} unused",d={},l=[{value:"Examples",id:"examples",level:2},{value:"How To Fix It",id:"how-to-fix-it",level:2}];function c(e){const r={admonition:"admonition",code:"code",h1:"h1",h2:"h2",header:"header",li:"li",p:"p",pre:"pre",table:"table",tbody:"tbody",td:"td",th:"th",thead:"thead",tr:"tr",ul:"ul",...(0,o.R)(),...e.components};return(0,i.jsxs)(i.Fragment,{children:[(0,i.jsx)(r.header,{children:(0,i.jsxs)(r.h1,{id:"rego_compile_error-assigned-var-name-unused",children:[(0,i.jsx)(r.code,{children:"rego_compile_error"}),": assigned var ",(0,i.jsx)(r.code,{children:"{name}"})," unused"]})}),"\n",(0,i.jsx)(r.p,{children:"This error is caused by a variable being assigned a value, but never used. This is often the result of a typo or\nmis-named variable being assigned."}),"\n",(0,i.jsx)(r.admonition,{type:"note",children:(0,i.jsxs)(r.p,{children:["This error is only shown when running OPA with the ",(0,i.jsx)(r.code,{children:"--strict"})," (or ",(0,i.jsx)(r.code,{children:"-S"}),") flag."]})}),"\n",(0,i.jsxs)(r.table,{children:[(0,i.jsx)(r.thead,{children:(0,i.jsxs)(r.tr,{children:[(0,i.jsx)(r.th,{children:"Stage"}),(0,i.jsx)(r.th,{children:"Category"}),(0,i.jsx)(r.th,{children:"Message"})]})}),(0,i.jsx)(r.tbody,{children:(0,i.jsxs)(r.tr,{children:[(0,i.jsx)(r.td,{children:(0,i.jsx)(r.code,{children:"parsing"})}),(0,i.jsx)(r.td,{children:(0,i.jsx)(r.code,{children:"rego_compile_error"})}),(0,i.jsx)(r.td,{children:(0,i.jsx)(r.code,{children:"rego_compile_error: assigned var {name} unused"})})]})})]}),"\n",(0,i.jsx)(r.h2,{id:"examples",children:"Examples"}),"\n",(0,i.jsx)(r.p,{children:"This a simple example of this error is when a variable is assigned a value, but never used:"}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-rego",children:'package policy\n\nimport future.keywords.if\n\nallow if {\n    user := input.user\n    input.user == "admin"\n}\n'})}),"\n",(0,i.jsxs)(r.p,{children:["When compiled, this will result in the following error because the variable ",(0,i.jsx)(r.code,{children:"user"})," is never used:"]}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-txt",children:"1 error occurred: policy.rego:6: rego_compile_error: assigned var user unused\n"})}),"\n",(0,i.jsx)(r.p,{children:"Another common reason for this message is when making a typo or mis-naming a variable. For example:"}),"\n",(0,i.jsx)(r.pre,{children:(0,i.jsx)(r.code,{className:"language-rego",children:'package policy\n\nimport future.keywords.contains\nimport future.keywords.if\n\ndeny contains message if {\n    input.user != "admin"\n\n    msg := "user is not admin"\n}\n'})}),"\n",(0,i.jsxs)(r.p,{children:["Here, the intent is that ",(0,i.jsx)(r.code,{children:"message"})," should be set to ",(0,i.jsx)(r.code,{children:"user is not admin"})," if the user is not an admin. However,\nthe variable ",(0,i.jsx)(r.code,{children:"msg"})," is assigned instead."]}),"\n",(0,i.jsx)(r.h2,{id:"how-to-fix-it",children:"How To Fix It"}),"\n",(0,i.jsx)(r.p,{children:"Based on the examples above, there are two main ways to fix this error:"}),"\n",(0,i.jsxs)(r.ul,{children:["\n",(0,i.jsxs)(r.li,{children:["Remove the assignments of unused variables. In the first example, remove the line ",(0,i.jsx)(r.code,{children:"user := input.user"}),".\nThe other option is to use the variable that was assigned. Often orphaned variables like this are the result\na refactoring and can be safely removed if they aren't making the rule more readable."]}),"\n",(0,i.jsxs)(r.li,{children:["Check for typos and mis-named variables. As well as normal typos, it's also easy to make mistakes and swap variables\nfor alternative names. For example, sometimes ",(0,i.jsx)(r.code,{children:"msg"})," instead of ",(0,i.jsx)(r.code,{children:"message"})," or ",(0,i.jsx)(r.code,{children:"user"})," instead of ",(0,i.jsx)(r.code,{children:"username"}),"."]}),"\n"]}),"\n",(0,i.jsxs)(r.p,{children:["While it's also possible to ignore this error by unsetting the ",(0,i.jsx)(r.code,{children:"--strict"})," (",(0,i.jsx)(r.code,{children:"-S"}),") flag, it's highly recommended to fix\nthe underlying issue as outlined in the points above."]})]})}function h(e={}){const{wrapper:r}={...(0,o.R)(),...e.components};return r?(0,i.jsx)(r,{...e,children:(0,i.jsx)(c,{...e})}):c(e)}}}]);

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.